Forum > General

split a string into TStringList

(1/2) > >>

ensimek:
Hi guys,
I've config.ini with line:
goals=Zakopane, Cracow, Warsaw, Busko Zdroj, Miedzyrzec

Split is done by this procedure:
procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Assert(Assigned(Strings)) ;
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;

The problem is that, when i assign Tstringlist to ComboBox I've
1. Zakopane.
2. Cracow
3. Warsaw
4. Busko
5. Zdroj
6. Miedzyrzec

But i should have only 5, 4 should be 'Busko Zdroj'

typo:
I can reproduce this here, like this:


--- Code: ---procedure TForm1.Button2Click(Sender:TObject);
var
  sl :tstringlist;
begin
  sl := tstringlist.create;
  try
    split(',', goals, sl);
    showmessage(sl.text);
  finally
    sl.free;
  end;
end;   
--- End code ---

Delimiter is not correctly considered.

But I can see that delimiter should be '=', which will do almost the same result:

1. Zakopane,
2. Cracow,
3. Warsaw,
4. Busko
5. Zdroj,
6. Miedzyrzec

Strange behavior.

ensimek:
The code looks almost like you reproduced.
Instead of ShowMEssage() I've:

Form3.ComboBox1.Items.Assign(miasta);

where miasta: TStringList;

Laksen:
You need StrictDelimiter set to true. It's false per default, which means it'll use space, linebreak and some others as delimiters too

--- Code: ---procedure Split (const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Assert(Assigned(Strings)) ;
   Strings.Clear;
   Strings.StrictDelimiter := true;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;

--- End code ---

ensimek:
Works great:) Thanks Laksen

Navigation

[0] Message Index

[#] Next page

Go to full version