Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing '#$A' from Delphi string

I am modifying a delphi app.In it I'm getting a text from a combo box. The problem is that when I save the text in the table, it contains a carriage return. In debug mode it shows like this.

newStr := 'Projector Ex320u-st Short Throw '#$A'1024 X 768 2700lm'

Then I have put

newStr := StringReplace(newStr,'#$A','',[rfReplaceAll]);

to remove the '#$A' thing. But this doesn't remove it.

Is there any other way to do this..

Thanks

like image 598
JCTLK Avatar asked Jun 21 '11 11:06

JCTLK


2 Answers

Remove the quotes around the #$A:

newStr := StringReplace(newStr,#$A,'',[rfReplaceAll]);

The # tells delphi that you are specifying a character by its numerical code. The $ says you are specifying in Hexadecimal. The A is the value.

With the quotes you are searching for the presence of the #$A characters in the string, which aren't found, so nothing is replaced.

like image 179
Marjan Venema Avatar answered Nov 16 '22 01:11

Marjan Venema


Adapted from http://www.delphipages.com/forum/showthread.php?t=195756

The '#' denotes an ASCII character followed by a byte value (0..255).

The $A is hexadecimal which equals 10 and $D is hexadecimal which equals 13.

#$A and #$D (or #10 and #13) are ASCII line feed and carriage return characters respectively.

Line feed = ASCII character $A (hex) or 10 (dec): #$A or #10

Carriage return = ASCII character $D (hex) or 13 (dec): #$D or #13

So if you wanted to add 'Ok' and another line:

Memo.Lines.Add('Ok' + #13#10)

or

Memo.Lines.Add('Ok' + #$D#$A)

To remove the control characters (and white spaces) from the beginning and end of a string:

MyString := Trim(MyString)

Why doesn't Pos() find them?

That is how Delphi displays control characters to you, if you were to do Pos(#13, MyString) or Pos(#10, MyString) then it would return the position.

like image 31
jcfaria Avatar answered Nov 15 '22 23:11

jcfaria