How to use TStrings.DelimitedText to separate a comma delimited string (Views: 100)
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.
ch: Character to scan for
S : String to scan
fromPos: first character to scan
Returns: position of next occurence of character ch, or 0, if none found
Description: Search for next occurence of a character in a string.
Error Conditions: none
Created: 11/27/96 by P. Below}
function IScan(ch: Char; const S: string; fromPos: Integer): Integer;
Result := 0;
for i := fromPos to Length(S) do
if S[i] = ch then
Result := i;
S: String to split
separator: character to use as separator between substrings
substrings: list to take the substrings
Isolates the individual substrings and copies them into the passed stringlist. Note that we only add to the list, we do not clear it first! If two separators follow each other directly an empty string will be added to the list.
will do nothing if the stringlist is not assigned
Created: 08.07.97 by P. Below}
procedure SplitString(const S: string; separator: Char; substrings: TStringList);
i, n: Integer;
if Assigned(substrings) and (Length(S) > 0) then
i := 1;
n := IScan(separator, S, i);
if n = 0 then
n := Length(S) + 1;
substrings.Add(Copy(S, i, n - i));
i := n + 1;
i > Length(S);
<< Back to main page