Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-07

Saving an array to a string

13 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Thompson
to Richard Mace
Is it possible to save an array of string to a single string for storing

in a string field within a database?

Suspect it'll be something like
MyString := '';
For i := Low(MyArray) to High(MyArray) Do
  MyString := MyString + MyArray[i] + LineEnding;
MyStringField.AsString := MyString;
Richard Mace wrote 16 lines
Hi All, Is it possible to save an array of string to a single string for storing in a string field within a database? If so, could anyone give me any pointers please? Thanks Richard -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/c927662e/attachment.html>

Reply by email

JB
to Michael Thompson

I use this code in my project:

      for i := 0 to length(Args) - 1 do
      begin
        sArgs := sArgs + VarToStr(Args[i]);
        if i < (length(Args) - 1) then
          sArgs := sArgs + ';';
      end;

Att.

JB

Jos? Benedito
JBS Solu??es
Consulting Systems Development
c
www.jbsolucoes.net

skype:

Michael Thompson wrote 37 lines
> Is it possible to save an array of string to a single string for storing in a string field within a database? Suspect it'll be something like MyString := ''; For i := Low(MyArray) to High(MyArray) Do MyString := MyString + MyArray[i] + LineEnding; MyStringField.AsString := MyString; On 1 July 2014 16:49, Richard Mace wrote: > Hi All, > Is it possible to save an array of string to a single string for storing > in a string field within a database? > If so, could anyone give me any pointers please? > > Thanks > > Richard > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > > -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/c7c1a35c/attachment.html>

Reply by email

Richard Mace
to Michael Thompson: in a string field within a database? Suspect it'll be something like MyString := ''; For…

Thanks Michael. And reading it back?

"Michael Thompson" wrote 37 lines
> Is it possible to save an array of string to a single string for storing in a string field within a database? Suspect it'll be something like MyString := ''; For i := Low(MyArray) to High(MyArray) Do MyString := MyString + MyArray[i] + LineEnding; MyStringField.AsString := MyString; On 1 July 2014 16:49, Richard Mace wrote: > Hi All, > Is it possible to save an array of string to a single string for storing > in a string field within a database? > If so, could anyone give me any pointers please? > > Thanks > > Richard > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus > > -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/d07596f3/attachment.html>

Reply by email

Michael Thompson
to Richard Mace

I'm lazy, I'd do it via a TStringList :-)

MyStringList := TStringList.Create;
MyStringList.Text := MyStringField.AsString;
SetLength(MyArray, MyStringlist.Count);
For i := 0 To MyStringList.Count-1 Do
  MyArray[i] := MyStringList[i];
MyStringList.Free;

In fact, I'd use a TStringList at both ends of the code, and not use an Array Of String at all... I like it when other code does my work for me :-)

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/fc1fb5dc/attachment.html>

Reply by email

Philippe
to Richard Mace: Thanks Michael. And reading it back? -------------- next part -------------- An HTML…

you can use

http://www.freepascal.org/docs-html/rtl/classes/tstringlist.html

var
lStrings : TStringList;
begin

....

 lStrings :=
TStringList.Create;
 with lStrings do
 begin
 delimiter :=
yourdelimiter; // don?t know if you can use "lineending"
strictdelimiter := true;
 DelimitedText := textfromyourfield;
 for n :=
0 to count - 1 do
 myarray[ n] := strings[ n ];
 end;
lStrings.free;

Philippe

Richard Mace wrote
Thanks Michael. And reading it back? On 1 Jul 2014 15:57,
"Michael Thompson" wrote
>> Is it

possible to save an array of string to a single string for storing in a string field within a database?

Show quoted text 6 lines
> > Suspect it'll be something like > MyString := ''; > For i := Low(MyArray) to High(MyArray) Do >
MyString := MyString + MyArray[i] + LineEnding;
> >
MyStringField.AsString := MyString;
> > On 1 July 2014 16:49,
Richard Mace wrote
> >> Hi All, >> Is it

possible to save an array of string to a single string for storing in a string field within a database?

>> If so, could anyone give me any

pointers please?

Show quoted text 7 lines
>> >> Thanks >> >> Richard >> >> -- >>

Reply by email

silvioprog
to Richard Mace: Is it possible to save an array of string to a single string for storing in a string…

2014-07-01 11:49 GMT-03:00 Richard Mace :

Show quoted text 7 lines
Hi All, Is it possible to save an array of string to a single string for storing in a string field within a database? If so, could anyone give me any pointers please? Thanks

You can use RUtils plugin:

https://github.com/silvioprog/rutils

So:

uses
  RUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
  ar: array of string;
begin
  // your array
  SetLength(ar, 2);
  ar[0] := 'abc';
  ar[1] := 'def';
  ar[2] := 'ghi';
  // array of string to string
  ShowMessage(RUtils.Implode(ar));
end;
Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/b7341fdc/attachment.html>

Reply by email

silvioprog
to Richard Mace: Thanks Michael. And reading it back? -------------- next part -------------- An HTML…

2014-07-01 12:04 GMT-03:00 Richard Mace :

Thanks Michael. And reading it back?

You can use RUtils plugin again:

uses
  RUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
  ar: array of string;
begin
  ar := RUtils.Explode('abc def ghi');
end;
Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/98a64ffe/attachment.html>

Reply by email

silvioprog
to Jürgen Hestermann

2014-07-01 12:24 GMT-03:00 J?rgen Hestermann :

Show quoted text 17 lines
Am 2014-07-01 17:14, schrieb silvioprog: procedure TForm1.Button1Click(Sender: TObject); > var > ar: array of string; > begin > // your array > SetLength(ar, 2); > ar[0] := 'abc'; > ar[1] := 'def'; > ar[2] := 'ghi'; > ar[2] will lead to an error if you set the length of the array to 2 (only indices 0 and 1 are allowed).

Oops, fast typing in reply. ^^'

...
SetLength(ar, 3);
...

So:

program project1;
{$mode objfpc}{$H+}
var
  s: string;
  ar: array of string;
begin
  // your array
  SetLength(ar, 3);
  ar[0] := 'abc';
  ar[1] := 'def';
  ar[2] := 'ghi';
  for s in ar do
    WriteLn(s);
end.

output:

abc
def
ghi

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/47ae63a1/attachment.html>

Reply by email

silvioprog
to Michael Thompson: That's brilliant. This answers a question I posted on the forum last week. Mind if I…

2014-07-01 12:21 GMT-03:00 Michael Thompson :

Show quoted text 8 lines
> You can use RUtils plugin: > https://github.com/silvioprog/rutils That's brilliant. This answers a question I posted on the forum last week. Mind if I quote your link in there? Mike

Feel free to send it! :-)

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140701/2c5ac3e2/attachment.html>

Reply by email