This is a working split method works really nicly I tested it well. Uses TStringList.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
procedure Split(const str1:string;const sep:string;LT:TStringList);
var i:Integer; {Holds the Real Pos}
t:AnsiString; {Holds the String}
i2:integer; {Holds the second pos}
begin
i:=1;
t:=str1;
repeat
t:=AnsiRightStr(t,Length(t)-i-Length(sep)+1);{Gets the right of the the string at the pos of the separator}
i:=AnsiPos (sep,t);{Gets the pos(Holds it)}
i2:=AnsiPos(sep,t); {The real pos(USes it)}
if AnsiLeftStr(t,i2-1)= '' then {Checks if the string is empty}
begin{Do nothing if is}
end
else
begin
LT.Add(AnsiLeftStr(t,i2-1)); {Adds to the TStringList}
end;
until i=0;{^Does above until the no more seperators}
end;
A little better implemation would be to make it a function, rather then a procedure. Like:
function Split(const str1:string;const sep:string): TStringList; var i:Integer; {Holds the Real Pos} t:AnsiString; {Holds the String} i2:integer; {Holds the second pos} begin Result := TStringList.Create; i:=1; t:=str1; repeat t:=AnsiRightStr(t,Length(t)-i-Leng th(sep)+1);{Gets the right of the the string at the pos of the separator} i:=AnsiPos (sep,t);{Gets the pos(Holds it)} i2:=AnsiPos(sep,t); {The real pos(USes it)} if AnsiLeftStr(t,i2-1)= '' then {Checks if the string is empty} begin{Do nothing if is} end else begin Result.Add(AnsiLeftStr(t,i2-1)); {Adds to the TStringList} end; until i=0;{^Does above until the no more seperators} end;
Usage:
mySplit := Split('wee%weee$wee', '$'); // mySplit being a TStringList of course. (If this comment was disrespectful, please report it.)
7/29/2004 12:54:19 AM:
i want delphi source code (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)