Solved

Text Strings

Posted on 1998-10-23
4
133 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

14 Experts available now in Live!

Get 1:1 Help Now