Solved

Text Strings

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 95
Delphi 2 69
Convert a string into a TDateTime 5 63
Installshield for Embarcadero EX 10.1 Berlin 4 60
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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