StringToToken
The StringToToken function splits the string S into blocks based on the given list of separator characters specified by aSeparators, and returns the items as a zero-based TStringList type.
You can then access individual items using the properties of the TStringList. Being zero-based, the first block of data is zero (0), the 2nd is block one (1) e.t.c.
The values identified by aSeparators must be in list notation mode, with square brackets ([ ]) at the beginning of the list, and at the end of the list, such as with the example shown below.
aSeparators can contain more than one separator character, such as [',', '.', ';', ':'], but only single characters can be used as elements within the list.
Empty token items are not output to the TStringList. For example, the instruction StringToToken('123||ABC', ['|']) returns only two strings in the list - being 123 and ABC. This means that StringToToken might not be suitable for decoding and re-encoding lists that can have empty items, such as a line from a text file that has separated field values - e.g. a CSV file - because any empty fields will be dropped from the field list.
Declaration: Function StringToToken( s : string; aSeparators : TFloSeparators) : TStringList;
The following examples illustrate some simple possibilities.
procedure
OnMapEvent(
var
Value:Variant);
var
AList, MyList, MyNextList, TheLastList : TStringList;
Str1, Str2, Str3, Str4 :
string
;
begin
AList := StringToToken(
'Abc|123||Xyx'
, [
'|'
]);
MyList := StringToToken(
'abc|123|456'
, [
'|'
]);
MyNextList := StringToToken(
'Simon, Peter, Nigel:Sally, Ethel:Brian, Florence, Zebedee'
, [
':'
]);
TheLastList := StringToToken(
'abcdef|Sue - Joe - Bob#"Why?", "When?": Peanuts + Snoopy + Woodstock'
, [
'#'
,
'|'
,
':'
]);
LogInfo(
'The 3rd section is effectively lost in translation - there is nothing between the pipes . . . '
);
Str1 := AList[
0
];
//returns 'Abc'
Str2 := AList[
1
];
//returns '123'
Str3 := AList[
2
];
//returns 'Xyz'
LogInfo(Str1); LogInfo(Str2);; LogInfo(Str3); LogInfo(
''
);
Str1 := MyList[
0
];
//returns 'abc'
Str2 := MyList[
1
];
//returns '123'
Str3 := MyList[
2
];
//returns '456'
LogInfo(Str1); LogInfo(Str2);; LogInfo(Str3); LogInfo(
''
);
Str1 := MyNextList[
0
];
//returns 'Simon, Peter, Nigel'
Str2 := MyNextList[
1
];
//returns 'Sally, Ethel'
Str3 := MyNextList[
2
];
//returns 'Brian, Florence, Zebedee'
LogInfo(Str1); LogInfo(Str2);; LogInfo(Str3); LogInfo(
''
)
Str1 := TheLastList[
0
];
//returns 'abcdef'
Str2 := TheLastList[
1
];
//returns 'Sue - Joe - Bob'
Str3 := TheLastList[
2
];
//returns '"Why?", "When?"'
Str4 := TheLastList[
3
];
//returns 'Peanuts + Snoopy + Woodstock'
LogInfo(Str1); LogInfo(Str2);; LogInfo(Str3); LogInfo(Str4); LogInfo(
''
)
end
;