Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to remove characters from a string Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
23-Sep-02
Category
Object Pascal-Strings
Language
Delphi All Versions
Views
77
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

We need a workable function that can strip embedded characters (single qoutes, 
double quotes, etc.,) from within string vars.

Answer:

Solve 1:

Here is a general method to remove characters from a string:


1   type
2     TCharSet = set of Ansichar;
3   
4   procedure RemoveCharacters(var S: AnsiString; const characters: TCharset);
5   var
6     i: Integer;
7   begin
8     for i := Length(S) downto 1 do
9       if S[i] in characters then
10        delete(S, i, 1);
11  end;
12  
13  
14  //In your case you would call it as:
15  
16  
17  RemoveCharacters(aString, [' ']);
18  
19  
20  //There are certainly faster ways to implement this but unless you call the 
21  procedure some ten-thousand times in a loop I would not worry about that.


Solve 2:
22  
23  function RemoveCharsFromString(const TheString: string; const CharsToRemove: array
24    of Char): string;
25  var
26    i:
27    Integer;
28  begin
29    Result := TheString;
30    for i := Low(CharsToRemove) to High(CharsToRemove) do
31    begin
32      Result := StringReplace(Result, CharsToRemove[i], '', [rfReplaceAll]);
33    end;
34  end;



Solve 3:

35  type
36    TSetOfChar = set of char;
37  
38  function RemoveCharsFromString(const TheString: string;
39    const CharsToRemove: TSetOfChar): string;
40  var
41    i, j: Integer;
42  begin
43    SetLength(Result, length(TheString));
44    j := 0;
45    for i := 1 to length(TheString) do
46    begin
47      if not (TheString[i] in CharsToRemove) then
48      begin
49        inc(j);
50        Result[j] := TheString[i];
51      end;
52    end;
53    SetLength(Result, j);
54  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC