How to Parse a Delimited String Into a String List
2005-11-21 19:20
295 查看
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:
~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ParseDelimited(const sl : TStrings; const value : string; const delimiter : string) ;
var
dx : integer;
ns : string;
txt : string;
delta : integer;
begin
delta := Length(delimiter) ;
txt := value + delimiter;
sl.BeginUpdate;
sl.Clear;
try
while Length(txt) > 0 do
begin
dx := Pos(delimiter, txt) ;
ns := Copy(txt,0,dx-1) ;
sl.Add(ns) ;
txt := Copy(txt,dx+delta,MaxInt) ;
end;
finally
sl.EndUpdate;
end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Usage (fills in Memo1) :
ParseDelimited(Memo1.lines,'Zarko;Gajic;;DelphiGuide',';')
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:
~~~~~~~~~~~~~~~~~~~~~~~~~
procedure ParseDelimited(const sl : TStrings; const value : string; const delimiter : string) ;
var
dx : integer;
ns : string;
txt : string;
delta : integer;
begin
delta := Length(delimiter) ;
txt := value + delimiter;
sl.BeginUpdate;
sl.Clear;
try
while Length(txt) > 0 do
begin
dx := Pos(delimiter, txt) ;
ns := Copy(txt,0,dx-1) ;
sl.Add(ns) ;
txt := Copy(txt,dx+delta,MaxInt) ;
end;
finally
sl.EndUpdate;
end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Usage (fills in Memo1) :
ParseDelimited(Memo1.lines,'Zarko;Gajic;;DelphiGuide',';')
相关文章推荐
- 【Z一贴】C++ String: How to convert a string into a numeric type?
- How to group anagrams in a string into an array
- How to read a whole document content into a string one-time
- how to query for a list<String> in jdbctemplate?--转载
- 如何把string解析为int?[C#] How to Parse a string to an int? [C#]
- How to convert XML String into XML document
- How to export Gobla Address List (GAL) into an Excel file for Outlook 2010
- How to convert Java String into byte[]?
- How to convert a list of tensors into a tensor
- How To Install Custom Component in Delphi (into existing package).
- [Linux cmd line] How to convert a binary file into output in C include file style.
- ParseResult Exception:java.lang.IllegalStateException: Cannot add header view to list -- setAdapter
- how to split string in c++
- Parse Json Array String to C# Object Array
- listToString
- How to be a Winner : Advice for students starting into research work
- 编写高质量代码改善java程序的151个建议——[52-57]String !about String How to use them?
- How to get projects list in eclipse
- How to enter text into a textfield in a web view using UIAutomation
- How To Load CLOB Data from a File into a CLOB column using PL/SQL