kevinjd
asked on
Stripping a character from a string
I simply need to strip a character, which is declared as a variable, from a string.
So if there's a string variable containing "Test123", I'd want it to strip all occurences of 2, leaving "Test13"
Any help would be appreciated!
Thanks
So if there's a string variable containing "Test123", I'd want it to strip all occurences of 2, leaving "Test13"
Any help would be appreciated!
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function StripChar(var Str: String; C: Char): Integer;
var dwOffset: Integer;
dwIndex: Integer;
dwLength: Integer;
begin
// Set starting offset
dwOffset:=0;
// Get length of string
dwLength:=Length(Str);
// Walk the string
for dwIndex:=1 to dwLength do
begin
// Check char
if (Str[dwIndex] = C) then
// Increment the offset
Inc(dwOffset)
// Check current offset
else if (dwOffset > 0) then
// Set char at offset position
Str[dwIndex-dwOffset]:=Str
end;
// Set new length of string
SetLength(Str, dwLength-dwOffset);
// Result is new length of string
result:=dwLength-dwOffset;
end;
Regards,
Russell