16

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

1
  • 8
    You might want to consider removing #$D too while you are at it. Commented Jun 21, 2011 at 11:34

2 Answers 2

46

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot... I will mark this correct tomorrow.coz gotta leave now.
@Andreas, I totally agree that this is a very good answer absolutelly deserving to be accepted. But, in general it is a good idea and it is SO good practice, too, just wait some longer to see if a simpler, more complete, better explained answer is posted.
Yep.. I was in a rush to leave.Therefore didn't have 5 mins to mark this. So I have done today.. Thanks alot. @Andreas : I guess this answers your question. :)
6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.