SRL Archive - OSR Help

String components?


Flight

2012-04-25 06:56:25

I was wondering if anyone knows the way to break down a single string into individual components? I've been looking around and I can't seem to find anything of use. Although I did stumble across the 'TStringList' type, although I'm not completely sure where it's used and what it's used for.

What would really be wonderful is a function to extract all the string characters from a single string and output a TStringArray containing each character. Also a function to do just the opposite.

An example would be:

Procedure FunWithStrings;
var
Text,NewStr: String;
StrComps: TStringArray;
begin
Text := 'Hey';
StrComps := ExtractString(Text); //This would return ['H', 'e', 'y']
NewStr := CompressStrAr(StrComps); //This would return 'Hey'
end;

Sometimes it's easier to explain something through code. :p

Markus

2012-04-25 07:00:07

I take it you have a very good reason for not accessing it @ char level (a string is an array, too!), but here you go:

function ExtractString(str : string) : TstringArray;
var i : integer;
begin
SetLength(result, length(str));
for i := 1 to high(str) do
result[i-1] := str[i];
end;

function CompressStrAr(StrComps : TStringArray) : string;
var i : integer;
begin
result := '';
for i := 0 to high(StrComps) do
result := result + StrComps[i];
end;
Untested, uncompiled.

Flight

2012-04-25 07:14:10

Perfecto'. Yeah I'm playing around with alternative RS text-finding. Thanks Markus. :)

masterBB

2012-04-25 07:54:47

Procedure FunWithStrings;
var
Text: String;
begin
Text := 'Hey';
Writeln(Text[1]);
Writeln(Text[2]);
Writeln(Text[3]);
end;

I don't understand why just don't use this.