Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treating strings.

Status
Not open for further replies.

ivoestg

Programmer
Mar 21, 2001
77
0
0
PT
Hi,

I need to open a txt file, read the first line of it:

Then i need to split that line into various strings, ex:

EX:TXT File
(1rst line)
0;400;100,100;D;400,400;D;400

Need to put it in an array in the this order, spliting it by the coma.:

strings[0]:='0;400;100'
strings[1]:='100;D;400'
strings[2]:='400;D;400'

How can i do this. thanks...

I'm in ivoestg@yahoo.com
 
I use to prosess comma or tab separated strings by characters.
For each line in file do:

Code:
readln(atextfile,astring){read a line}
col:=0;{reset column}
for i:= 1 to lengt(astring) do{each char in string}
begin
  if astring[i]=',' then inc(col) else{found separator}
  strings[col]:=strings[col]+astring[i];{store char}
end;

Not tested, just to show the idea!
 
Try this function. You need to pass it the delimiter character, the string you wish to parse and the instance of the delimiter you wish to find. You can then check for a return of '' to see if you tried to parse one too many times.

...
String[ 0 ] := PosParse( ',', sString, 1 );
String[ 1 ] := PosParse( ',', sString, 2 );
String[ 2 ] := PosParse( ',', sString, 3 );
...

function PosParse( sDelimiter, sString : string; iTimes : integer ) : string;
var
i, iPos, iDelLen : integer;
sResult : string;
begin
iDelLen := Length( sDelimiter );
sResult := sString;

for i := 1 to iTimes - 1 do
begin
iPos := Pos( sDelimiter, sResult );
if ( iPos > 0 ) then
sResult := Copy( sResult, iPos + iDelLen, Length( sResult ) - iPos )
else if ( i <= iTimes - 1 ) then
begin
sResult := '';
Break;
end;
end;

iPos := Pos( sDelimiter, sResult );
if ( iPos > 0 ) then
sResult := Copy( sResult, 1, iPos - 1 );

PosParse := sResult;
end;
 
If you use stringlists, the process is automatic and the array is built for you:

ts := TstringList.Create;
ts.CommaText := '0;400;100,100;D;400,400;D;400';
// or ts.CommaText := YourStringVariable

Now you have:
ts[0] is '0;400;100'
ts[1] is '100;D;400'
.
.
.
etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top