?
Solved

How to Remove #0 Char : Delphi

Posted on 2007-07-31
9
Medium Priority
?
10,221 Views
Last Modified: 2008-01-09
I have a string variable with "nul" ie. #0 char repeatedly coming up. How can i remove all #0 Chars.
0
Comment
Question by:qanuj
9 Comments
 

Author Comment

by:qanuj
ID: 19600609
May be Adding more points would help
0
 
LVL 10

Expert Comment

by:dinilud
ID: 19600650
Can you explain more.
0
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 1500 total points
ID: 19601785
Looks like StringReplace() doesn't work with #0 chars (if it worked, you could use "s := StringReplace(s, #0, '', [rfReplaceAll]);")...

You may remove #0 chars in 2 ways:
1) go through string and use Delete() to remove "parts of string", i.e., #0 chars (see an example below)
2) go through string and copy parts between #0 chars from "input" string to "output" string

1st way could be slow if you work with long strings that have lots of #0 chars, because each time you use Delete(), part of string has to be moved in memory. For example:
"aaa0bbb0ccc0ddd"
When you remove first "0", "bbb0ccc0ddd" will be moved.
When you remove second "0", "ccc0ddd" will be moved.
etc. So many parts of string get copied multiple times, which is not good (although it's easier to do).

2nd way should be faster, because you copy "aaa", then "bbb", then "ccc" and then "ddd" without touching other parts of string (have to say here that I'm not sure how Delphi "strings" work though, maybe it will copy all the string when you update it; in that case 1st way could be better).



Here's an example with using Delete() to remove #0 chars:

procedure RemoveNull (var str : string);
var
  n : integer;
begin
n := 1;
while n <= Length(str) do begin
  if str[n] = #0 then begin
    Delete(str, n, 1);
    Continue;
  end;
  inc(n);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  s : string;
begin
s := 'one'#0'two'#0'three';
//s := StringReplace(s, #0, '', [rfReplaceAll]); // doesn't work
RemoveNull(s); // works fine
ShowMessage(s);
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Russell Libby
ID: 19602107
Another alternative

Regards,
Russell

-----

example usage:

var  S:             String;
begin

  S:=#0'Hello'#0#0' World'#0' this is a test'#0;
  RemoveChar(S, #0);
  Edit1.Text:=S;

end;

// Procedure to remove a specified char from a string variable
procedure RemoveChar(var S: String; Ch: Char);
var  dwShift:       Integer;
     dwIndex:       Integer;
begin

  // Check string length
  if (Length(S) > 0) then
  begin
     // Set shift
     dwShift:=0;
     // Walk the string
     for dwIndex:=1 to Length(S) do
     begin
        // Check for char
        if (S[dwIndex] = Ch) then
           // Increment the shift
           Inc(dwShift)
        // Determine if shift is in effect, if not then no move needs to be done
        else if (dwShift > 0) then
           // Move char to shifted position
           S[dwIndex - dwShift]:=S[dwIndex];
     end;
     // Check for total shift, update string length if required
     if (dwShift > 0) then SetLength(S, Length(S) - dwShift);
  end;

end;
0
 
LVL 4

Expert Comment

by:JeePeeTee
ID: 19637505
Shorty:

procedure RemoveChar(var S: String; Ch: Char);
var
  I:Integer;
begin
  if Length(S) = 0 then
    Exit;

  for I := (Length(S) - 1) downto 0 do begin
    if (S[I] = Ch) then
      Delete(S, I, 1);
  end;
end;
0
 
LVL 10

Expert Comment

by:dinilud
ID: 19653538
i prepose rllibby's solution
0
 
LVL 4

Expert Comment

by:JeePeeTee
ID: 19668412
When iterating through data and removing some I prefer to work from end-to-begin instead of begin-to-end.
0
 

Expert Comment

by:momo209
ID: 19743856
Try this

Function RemoveNull(var str : string):string;
var
  i:integer;
  temp:string;
begin
  if length(str)>0 then
  begin
    for i:=1 to length(str) do
    if str[i]<>#0 then temp:=temp+str[i];
    RemoveNull:=temp;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  s : string;
begin
  s := 'one'#0'two'#0'three';
  ShowMessage(RemoveNull(s));
end;
0
 
LVL 4

Expert Comment

by:JeePeeTee
ID: 19744695
Or even shorter...

function RemoveNull(AString: string) : string
 var s : string;
begin
  s := AString;
  while Pos(s, #0) > 0 then
    Delete(s, Pos(s, #0), 1);

  result := s;
end;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

864 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