How to Parse a Delimited String Into a String List

How to Parse a Delimited String Into a String List

We are searching data for your request:

Forums and discussions:
Manuals and reference books:
Data from registers:
Wait the end of the search in all databases.
Upon completion, a link will appear to access the found materials.

There are many times when you need to split a string into an array of strings by using a character as a separator. For example, a CSV ("comma" separated) file might have a line like "Zarko;Gajic;;DelphiGuide" and you want this line to be parsed into 4 lines (strings) "Zarko", "Gajic", "" (empty string) and "DelphiGuide" using the semi-colon character ";" as a delimiter.

Delphi provides several methods to parse a string, but you might find that neither one does exactly what you need. For example, the ExtractStrings RTL method always uses quote characters (single or double) for delimiters. Another approach is to use the ​Delimiter and ​DelimitedText properties of the TStrings class-but unfortunately, there is a bug in the implementation ("inside" Delphi) where the space character is always used as a delimiter.

The only solution to parsing a delimited string is to write a method of your own:

Delimited String Example

procedure ParseDelimited(const sl : TStrings; const value : string; const delimiter : string) ;
dx : integer;
ns : string;
txt : string;
delta : integer;
delta := Length(delimiter) ;
txt := value + delimiter;
while Length(txt) > 0 do
dx := Pos(delimiter, txt) ;
ns := Copy(txt,0,dx-1) ;
sl.Add(ns) ;
txt := Copy(txt,dx+delta,MaxInt) ;

Usage (fills in Memo1) :


  1. Finghin

    Well, well, it is not necessary to speak so.

  2. Vikasa

    I believe that you are making a mistake. I can prove it.

  3. Ruff

    Excuse the question is far away

  4. Wiellaby

    As the expert, I can assist. I was specially registered to participate in discussion.

  5. Sang

    It is a pity, that now I can not express - it is very occupied. I will return - I will necessarily express the opinion.

Write a message