Important alert: (current site time 7/16/2013 5:46:53 AM EDT)
 

article

A working split method

Email
Submitted on: 7/15/2004 1:03:39 AM
By: *Unknown* 
Level: Beginner
User Rating: By 2 Users
Compatibility: Delphi 7
Views: 13253
(About the author)
 
     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...   
  1. 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.
  2. 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.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				Interface

uses SysUtils, Classes,Dialogs, Sockets,StrUtils, StdCtrls;

procedure Split(const str1:string;const sep:string;LT:TStringList);


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;


Usage:

Declares:

l:TStringList;

Code:

l:=TStringList.Create();
Split('€0À€213À€0À€187À€0À€7À€Example:!€10À€0À€11À€0À€17À€0À€138À€1À€13À€1À€60À€À€184À€À€192À€-1À€10001À€130À€10002À€0À€198À€0À€213À€0À€7À€EmeÀ€10À€999À€11À€0À€17À€0À€137', '0À€7À€',l);

MessageBox(0,PChar(l.Strings[1]),0,0);{Shows}

Closing:
l.Destroy();{I dont know if needed.}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/23/2004 3:05:49 AMBrandon Pawoll

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.)
 

To post feedback, first please login.