Handy dandy string methods.

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Handy dandy string methods.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:24 pm

by Carl Ranson >> Thu, 4 May 2000 21:23:48 GMT

Hi all.

I've had these two methods on the string primitive for ages. They've proven to be really useful.

for instance you can do things like...

var
s : String;
begin

s := 'This is a test string';
write s.before('test'); // "This is a "
write s.after('This '); // "is a test string"
s := 'Want to change this number (xxx) to something else';
write s.after("(").before(")"); // writes "xxx"
s := s.before("(") & "(" & "YYY" & ")" and s.after(")"); // changes xxx to YYY
end;

hope you find them helpful.

CR


before(delimeter : String) : String;

vars
index : Integer;begin

index := self.pos(delimeter,1);
if index = 0 then
return self;
else
return self[1:index-1];
endif;
end;

after(delimeter : String) : String;

vars
index : Integer;begin

index := self.pos(delimeter,1);
if index = 0 then
return null;
else
return self[index+delimeter.length:end];
endif;
end;

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Handy dandy string methods.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:24 pm

by Carl Ranson >> Thu, 4 May 2000 21:52:40 GMT

woops, my version control isn't as good as it should be, the after method contains a bug.

it should have been this...

after(delimeter : String) : String;

vars
index : Integer;begin

index := self.pos(delimeter,1);
if index = 0 then
return null;
else
if index+delimeter.length > self.length then
return null;
else
return self[index+delimeter.length:end];
endif;
endif;
end;

CR


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 31 guests