Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2008, 10:37 AM
kim_ray kim_ray is offline
Member
 
Join Date: Aug 2007
Posts: 58
Default spliting the sting by more then one charcter

hi,

i am trying to split string this is my function:
[delphi]
function TForm1.split(thing: string; delimeter: string): TStringList;
var
buffer: string;
begin
Result := TStringlist.Create;
repeat
begin
if (copy(thing,1,1) = copy(delimeter,1,1)) and (copy(thing,1,length(delimeter)) = delimeter) then begin
Result.Add(buffer);
buffer := '';
thing := copy(thing,2,length(thing));
end
else begin
buffer := buffer + copy(thing,1,1);
thing := copy(thing,2,length(thing));
end;
if thing = '' then begin
buffer := buffer + thing;
Result.Add(buffer);
buffer := '';
thing := '';
//end;
end;
end;
until thing = '';
end;
[/delphi]

but when i try to split it :

a := aaa#|bbbb#|cccc#|dddd
capspaese := split(a, '#|');
//ShowMessage(capspaese1[0]);
ShowMessage(capspaese1[1]);
//ShowMessage(capspaese1[2]);


then it shows : |bbb and

this is removing only one character when funtion try to split the string plz check my code and how will i improve this code

plz suggest me




kim_ray
Reply With Quote
  #2  
Old 11-29-2008, 12:06 PM
develyoy develyoy is offline
Senior Member
 
Join Date: Nov 2007
Posts: 628
Default RE: spliting the sting by more then one charcter

First of all:
- indenting! keep order in your code, it makes code more readable, especially to yourself!
- leave object-creating/freeing up to the same scope, preferably to a 'scope above' if a procedure only manipulates objects.
- don't depend on us to make your homework. I suspect you will only copy paste this code, but I hope you will read it very closely and see what it does, and how it works, so you can learn from it.
- though Delphi allows to change the local copy of parameter values, it's generally agreed upon this is not a good idea. Espacially with strings, it's always better to Copy several times, preferably to the final destination you want the data to go to.

[DELPHI]
procedure Split(Data,Delimiter:string;Lines:TStrings);
var
i,j,k,l1,l2:integer;
begin
l1:=Length(Data);
l2:=Length(Delimiter);
i:=1;
while (i<=l1) do
begin
j:=i;
k:=0;
while (i<=l1) and (k if Data[i+k]=Delimiter[1+k] then inc(k) else
begin
k:=0;
inc(i);
end;
Lines.Add(Copy(Data,j,i-j));
inc(i,k);
end;
end;
[/DELPHI]
Reply With Quote
  #3  
Old 11-29-2008, 12:16 PM
chris_w chris_w is offline
Senior Member
 
Join Date: Jan 2004
Posts: 1,397
Default RE: spliting the sting by more then one charcter

[pre]
You really don't need a function to this...

[/pre][DELPHI]
YourStringList.Text := StringReplace(YourString, '#|', #10, [rfReplaceAll]);

[/DELPHI][pre]
"There is a theory which states that if ever anybody discovers
exactly what the Universe is for and why it is here, it will
instantly disappear and be replaced by something even more
bizarre and inexplicable. There is another theory which states
that this has already happened."
-- Douglas Adams
[/pre]

Chris
Reply With Quote
  #4  
Old 11-29-2008, 12:44 PM
kim_ray kim_ray is offline
Member
 
Join Date: Aug 2007
Posts: 58
Default RE: spliting the sting by more then one charcter

thanx for ur reply


but this is copy and paste plz suggest me the function not procedure and this is copy the string.

kim_ray
Reply With Quote
  #5  
Old 11-29-2008, 12:57 PM
kim_ray kim_ray is offline
Member
 
Join Date: Aug 2007
Posts: 58
Default RE: spliting the sting by more then one charcter

thanx for ur reply but i dont want to replace the string

i am passing a string and thn retrn back tstringlist or an array

kim_ray
Reply With Quote
  #6  
Old 11-29-2008, 01:47 PM
chris_w chris_w is offline
Senior Member
 
Join Date: Jan 2004
Posts: 1,397
Default RE: spliting the sting by more then one charcter

I don't know what I was thinking. Now I am thinking that you don't know what StringReplace() does.

Chris
Reply With Quote
  #7  
Old 11-29-2008, 06:55 PM
dvio dvio is offline
Member
 
Join Date: Dec 2005
Posts: 46
Default RE: spliting the sting by more then one charcter

You can do it by applying chris_w suggestion.

exp:

[DELPHI]function split(thing, delimeter: string): TStringList;
begin
result := TStringList.Create;
result.text := StringReplace(thing, delimeter, #10, [rfReplaceAll]);
end;[/DELPHI]

but this is not safe, the safer one is:

[DELPHI]procedure split(thing, delimeter: string; SL : TStrings);
begin
SL.Text := StringReplace(thing, delimeter, #10, [rfReplaceAll]);
end;[/DELPHI]

usage :

[DELPHI]split('aaa#|bbbb#|cccc#|dddd','#|', ListBox1.Items);[/DELPHI]

regards,

dvio

If it's useful, than accept chris_w suggestion. I only make it brighter for you
Reply With Quote
  #8  
Old 11-29-2008, 11:42 PM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: spliting the sting by more then one charcter

Use this procedure:

[DELPHI]procedure SplitString(thing: string; delimeter: string; oSL : TStringList);
begin
oSL.Text := StringReplace(thing,delimeter,Char(13)+Char(10),[rfReplaceAll, rfIgnoreCase]);
end;


//Example Of use:

var
a : String;

oSL : TStringlist;
begin
a := 'aaa#|bbbb#|cccc#|dddd';

oSL := TStringlist.Create;

SplitString(a,'#|',oSL);


Showmessage(oSL[0]);
Showmessage(oSL[1]);
Showmessage(oSL[2]);
Showmessage(oSL[3]);

oSL.Free;[/DELPHI]

Regards,
Abdulaziz Jasser
Reply With Quote
  #9  
Old 11-30-2008, 04:33 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default RE: spliting the sting by more then one charcter

If you do not know the number of the strings then uses a loop. Example:


[DELPHI]var
a : String;
i : Integer;

oSL : TStringlist;
begin
a := 'aaa#|bbbb#|cccc#|dddd';

oSL := TStringlist.Create;

SplitString(a,'#|',oSL);

for i:=0 to oSL.Count-1 do
Showmessage(oSL[i]);

oSL.Free;[/DELPHI]

Regards,
Abdulaziz Jasser
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:28 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.