Link to home
Start Free TrialLog in
Avatar of kevinjd
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
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Russell Libby
Answer is closed, but just wanted to offer another alternative (char stripping). Its about 4-5x faster:

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[dwIndex];
  end;

  // Set new length of string
  SetLength(Str, dwLength-dwOffset);

  // Result is new length of string
  result:=dwLength-dwOffset;

end;

Regards,
Russell