[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Stripping a character from a string

Posted on 2005-04-26
3
Medium Priority
?
355 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:kevinjd
3 Comments
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 252 total points
ID: 13866701
StringReplace('Test123', '2', '', [rfReplaceAll])
0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 248 total points
ID: 13866882
procedure TForm1.Button1Click(Sender: TObject);
var
  str: string;
begin
  str := 'Test123';
  str := StringReplace(str, '2', '', [rfReplaceAll]);
  ShowMessage(str);
end;

Regards, Geo
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13867022
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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question