1

I want to add 30 different strings into a stringList . I do not want to add AList.Items.Add 30 times. Nor do i want to keep the strings in an array and run a loop. I was thinking may be i could write a single AList.Add ( not in a loop) where the strings to be added were seperated by a Delimiter . e.g. AList.Add('Data1' + <Delim> + 'Data2' ...)

How to do that ? Please note that i am just curious as to if it can be done this way. It is quite ok if not as there are better ways to accomplish this. ( keeping the strings in an array and using a loop to add data is my idea)

Thanks in Advance

4 Answers 4

12

You can write a procedure that does this:

procedure SLAddStrings(SL: TStrings; S: array of string);
var
  i: Integer;
begin
  SL.BeginUpdate;
  for i := low(S) to high(S) do
    SL.Add(S[i]);
  SL.EndUpdate;
end;

Try it:

var
  SL: TStringList;
begin
  SL := TStringList.Create;
  SLAddStrings(SL, ['car', 'cat', 'dog']);
Sign up to request clarification or add additional context in comments.

3 Comments

it requires the use of a string array , i was curious if it could be done in a single add statement without any array stuff.
@Cyprus: Of course, you simply have to parse the string first.
What's important here is not that you use a solution that fits your pre-conceptions as to what the solution might be. What's important is what works efficiently. This answer, using an open array parameter is the solution to your problem.
7

Create a temporary TStringList, assign the string to its DelimitedText property, pass the temporary to the AddStrings() method of the destination TStringList, and then free the temporary.

var
  Temp: TStringList;
begin
  Temp := TStringList.Create;
  try
    Temp.Delimiter := <Delim>;
    // if using a Delphi version that has StrictDelimiter available:
    // Temp.StrictDelimiter := True;
    Temp.DelimitedText := 'Data1' + <Delim> + 'Data2' ...;
    AList.AddStrings(Temp);
  finally
    Temp.Free;
  end;
end;

1 Comment

apart from a little more memory consumption , this will be faster than the rest , i think
6

Just use DelimitedText property. E.g. if your delimiter is set to , (default in TStringList) then you can write this code:

AList.DelimitedText := 'Data1,Data2';

7 Comments

But of course, this will remove all previous items in the list.
I do not have Delimited Text property , I am using D7
@AndreasRejbrand: AList.DelimitedText := AList.DelimitedText + ',Data1,Data2');
@CyprUS: D7 has a DelimitedText property.
@The_Fox: That would work. But it is not particularly optimized.
|
1

you can use TStringList.DelimitedText property to add text, wich uses your Delimiter char. TStringList will split your text and then you can access each string separately using strings property;

program Project3;
{$APPTYPE CONSOLE}
uses classes;

const DATA = 'one,two,three';

var sl : TStringList;
    s : string;
begin
    sl := TStringList.Create();
    try
        sl.Delimiter := ',';
        sl.DelimitedText := DATA;
        for s in sl do begin
            writeln(s);
        end;
        readln;
    finally
        sl.Free();
    end;
end.

and result is

one
two
three

5 Comments

But of course, this will remove all previous items in the list.
I do not have Delimited Text property , I am using D7.
@CyprUS You're searching in the wrong place probably, because TStringList's DelimitedText property is available in Delphi 7.
@teran : Sorry, i overlooked that.
sorry, I haven't noticed that question is about D7. Also DelimitedText with Delimiter = ',' equals CommaText property. (don't know about D7, but both of them exist in D2007)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.