Solved

Text Strings

Posted on 1998-10-23
4
135 Views
Last Modified: 2010-04-06
How do I do if I want to split a string in, lets say, three parts.
Each of the parts is separated by a semicolon, and the words should be able to have different lenghts.
one;two;three; or word1;blaha;hello
Each of the words should be put in a seperate string (like string1, string2, string3, or whatever...)
0
Comment
Question by:t0m3k
  • 2
4 Comments
 

Accepted Solution

by:
vperez earned 100 total points
ID: 1344156
Ok...use the Pos function to find the semicolons like so it will give you where the first semicolon is.  Then use Copy and Delete. Like so:

end := Pos(";",mainstring);
string1 := Copy(mainstring,0,end-1);
mainstring := Delete(mainstring,0,end-1);

end := Pos(";",mainstring);
string2 := Copy(mainstring,0,end-1);
mainstring := Delete(mainstring,0,end);

Do this as many times as it takes...
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1344157
var cTotal,cSub1,cSub2,cSub3:string;
var nPos:integer;
begin
  cTotal:='one;two;three';
  cSub1:=cTotal;
  cSub2:='?';
  cSub3:='?';
  nPos:=Pos(';',cTotal)    ;
  if nPos>0 then begin
    cSub1:=copy(cTotal,1,nPos-1);
    cTotal:=Copy(cTotal,nPos+1,255);
    cSub2:=cTotal;
    nPos:=Pos(';',cSub2);
    if nPos>0 then begin
       cSub2:=copy(cTotal,1,nPos-1);
       cSub3:=Copy(cTotal,nPos+1,255);
    end;
  end;
  ShowMessage(cSub1);
  ShowMessage(cSub2);
  ShowMessage(cSub3);
end;

0
 

Expert Comment

by:vperez
ID: 1344158
well no offense or anything but mine is simpler...
and you can put it in a loop like so

i := 1;
Do
     i := i + 1;
     end := Pos(";",mainstring);
     string[i] := Copy(mainstring,0,end-1);
     mainstring := Delete(mainstring,0,end-1);
while(Pos(";",mainstring) = 0);
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1344159
Here is how it is done....

Function Parse(TheItemStr, ParseStr : String; ItemNum : Integer): String;
 Var
  I4 : Integer;
  S4 : String;
Begin
TheItemStr := TheItemStr + ParseStr;
  S4 := '';
 I4 := 0;
 While I4 <= ItemNum Do
  Begin
   S4 := Copy(TheItemStr, 1, Pos(ParseStr, TheItemStr) - 1);
   Delete(TheItemStr, 1, Pos(ParseStr, TheItemStr));
   Inc(I4);
  End;
  Result := S4;
End;
Example call:
MyString := Parse(Memo1.Text, ';', 1);

Cheers,
Viktor
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi parse string to params 3 121
how to send memory stream from ics Client To ics server ? 11 104
control image tags in a string ? 12 127
Dev express lookupcombo 3 26
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now