Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
How to use TStrings.DelimitedText to separate a comma delimited string Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Object Pascal-Strings
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Jonas Bilinkevicius

I am trying to use TStrings.DelimitedText to separate a comma delimited string. The 
trouble is (some of my strings contain spaces, for example a person's address), 
with TStrings.DelimitedText it seems to have the space character set as a delimiter 
as well as the character that I specify. How do I stop it from doing this?


The substrings in the comma-separated list have to be enclosed in the 
TStrings.QuoteChar for this to work properly. The way TStrings.SetDelimitedText has 
been written it will not only break on the Delimiter character but also on any 
character in the range #1..' ' when they appear outside a quoted string. The 
SplitString routine below does not suffer from this problem but it does not handle 
delimiters inside quoted strings.

1   {Function IScan
2   Parameters:
3   ch: Character to scan for
4   S : String to scan
5   fromPos: first character to scan
6   Returns: position of next occurence of character ch, or 0, if none found
7   Description: Search for next occurence of a character in a string.
8   Error Conditions: none
9   Created: 11/27/96 by P. Below}
11  function IScan(ch: Char; const S: string; fromPos: Integer): Integer;
12  var
13    i: Integer;
14  begin
15    Result := 0;
16    for i := fromPos to Length(S) do
17    begin
18      if S[i] = ch then
19      begin
20        Result := i;
21        Break;
22      end;
23    end;
24  end;
26  {Procedure SplitString
27  Parameters:
28  S: String to split
29  separator: character to use as separator between substrings
30  substrings: list to take the substrings
31  Description:
32  Isolates the individual substrings and copies them into the passed stringlist. Note 
33  that we only add to the list, we do not clear it first! If two separators follow 
34  each other directly an empty string will be added to the list.
35  Error Conditions:
36  will do nothing if the stringlist is not assigned
37  Created: 08.07.97 by P. Below}
39  procedure SplitString(const S: string; separator: Char; substrings: TStringList);
40  var
41    i, n: Integer;
42  begin
43    if Assigned(substrings) and (Length(S) > 0) then
44    begin
45      i := 1;
46      repeat
47        n := IScan(separator, S, i);
48        if n = 0 then
49          n := Length(S) + 1;
50        substrings.Add(Copy(S, i, n - i));
51        i := n + 1;
52      until
53        i > Length(S);
54    end;
55  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC