SRL Archive - SRL Archives

StartEnd() will check if a string starts/ends with what you specify


Ron

2006-12-31 17:47:54

Function code:

// check to see if a certain string starts AND ends with something.
// returns true if the above is true.
// Made by Ron :)
function StartEnd(Str, StartsWith, EndsWith: string): boolean;
begin
if (lowercase(copy(Str, 1, Length(StartsWith))) = lowercase(StartsWith)) and (lowercase(copy(Str, Length(Str) - Length(EndsWith) + 1, Length(Str))) = lowercase(EndsWith)) then
begin
Result := True;
end else
begin
Result := False;
end;
end;

Test function code:

program New;

const
Website = 'http://www.google.com';

// check to see if a certain string starts AND ends with something.
// returns true if the above is true.
// Made by Ron :)
function StartEnd(Str, StartsWith, EndsWith: string): boolean;
begin
if (lowercase(copy(Str, 1, Length(StartsWith))) = lowercase(StartsWith)) and (lowercase(copy(Str, Length(Str) - Length(EndsWith) + 1, Length(Str))) = lowercase(EndsWith)) then
begin
Result := True;
end else
begin
Result := False;
end;
end;

begin
ClearDebug;
if(StartEnd(Website,'http://','.com'))then
WriteLn('"'+Website+'" starts with http:// and ends with .com');
end.

~Ron

Freddy1990

2006-12-31 19:18:42

Ok, now tell me what this is useful for, lol :)

Ron

2006-12-31 20:51:30

lol well I used it in the stats grabber to see if what the user put in ended with a .ini and began with c:\

:D

Freddy1990

2007-01-01 06:00:56

I see, well you can however cut down the lines a bit :)
function StartEnd(Str, StartsWith, EndsWith: string): boolean;
begin
if (lowercase(copy(Str, 1, Length(StartsWith))) = lowercase(StartsWith)) and (lowercase(copy(Str, Length(Str) - Length(EndsWith) + 1, Length(Str))) = lowercase(EndsWith)) then
Result := True
else
Result := False;
end;

Ron

2007-01-01 11:12:54

Ya like you saw in the scar 2.04 thread, I didn't know how to do that.

So used to ending all my statements in semicolons. :D Thanks again for helping me with that!