SRL Archive - OSR Help

Substring help


Echo_

2011-07-23 08:14:02

So somebody asked me about a substring function. I didn't find one, so I made one myself.

function Substring(str: string; start, finish: Integer): string;
var
char1, char2: Char;
begin
char1 := StrGet(str, start);
char2 := StrGet(str, finish);
Result := Between(char1, char2, str);
end;

But when I execute this script:

program new;

const
RANDOM_STRING = 'This is a random string.';

function Substring(str: string; start, finish: Integer): string;
var
char1, char2: Char;
begin
char1 := StrGet(str, start);
char2 := StrGet(str, finish);
Result := Between(char1, char2, str);
end;

begin
Writeln(Substring(RANDOM_STRING, 1, 10));
end.

This is the result:

his

What am I doing wrong?

Wizzup?

2011-07-23 08:16:04

Why not use Copy() for this? Anyway your approach fails since character are not unique. A space is found right after ``This''.

Dgby714

2011-07-23 08:16:13


function substring(const d: string; const start, finish: integer);
begin
Result := Copy(d, start, finish - start + 1);
end;

Echo_

2011-07-23 08:23:27

I didn't even know about the Copy function :p Thanks guys

Maybe add it to the side panel in Simba under the string category?

Coh3n

2011-07-23 10:43:07

From: Echo_
I didn't even know about the Copy function :p Thanks guys

Maybe add it to the side panel in Simba under the string category?
It's built in to Pascal Script as far as I know. Same with methods like inc(), dec(), etc.