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 white-spaces 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
19-Oct-02
Category
Object Pascal-Strings
Language
Delphi 2.x
Views
34
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I need to be able to search through a list of strings and remove the ones that only 
contain what I call "white space" - spaces, tabs, control chars, etc.. Is there a 
function (either Delphi or WinAPI) that will do this?

Answer:

Solve 1:

1   procedure RemoveBlanks(sl: TStringList);
2   var
3     i, j: Integer;
4     blank: Boolean;
5     c: Char;
6     chars: array[Char] of Boolean;
7   begin
8     { Set all significant chars to false }
9     FillChar(chars, SizeOf(chars), True);
10    for c := 'A' to 'Z' do
11      chars[c] := False;
12    for c := 'a' to 'z' do
13      chars[c] := False;
14    for c := '0' to '9' do
15      chars[c] := False;
16    i := Pred(sl.Count);
17    while (i >= 0) do
18    begin
19      blank := True;
20      j := Length(sl[i]);
21      while (blank and (j >= 0)) do
22      begin
23        blank := blank and chars[sl[i][j]];
24        Dec(j);
25      end;
26      if blank then
27        sl.Delete(i);
28      Dec(i);
29    end;
30  end;



Solve 2:

31  procedure DeleteWhiteLines(Strings: TStrings);
32  var
33    I: Integer;
34  begin
35    for I := Strings.Count - 1 downto 0 do
36      if TrimLeft(Strings[I]) = '' then
37        Strings.Delete(I);
38  end;



Solve 3:

39  function KeepStr(sSource: string; ValidChars: TCharSet): string;
40  var
41    iCurPos: Integer;
42  begin
43    Result := Trim(sSource);
44    iCurPos := 1;
45    if Length(Result) > 0 then
46    begin
47      repeat
48        if Result[iCurPos] in ValidChars then
49          Inc(iCurPos)
50        else
51          Delete(Result, iCurPos, 1);
52        if length(Result) = 0 then
53          break;
54      until (iCurPos = Length(Result) + 1);
55    end;
56  end;
57  
58  //You use KeepStr like this:
59  
60  type
61    TCharSet = set of char;
62  
63  var
64    i: integer;
65    s: string;
66  begin
67    {AList is a TStringList declared somewhere}
68    {have to work from the end of the list}
69    for i := pred(AList) downto 0 do
70    begin
71      s := AList[i];
72      s := KeepStr(s, ['A'..'Z'] + ['a'..'z'] + ['0'..'9']);
73      if s = '' then
74        AList.Delete(i);
75    end;
76  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