string 从 Delphi 字符串中删除 '#$A'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6424510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing '#$A' from Delphi string
提问by JCTLK
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.
我正在修改一个 delphi 应用程序。在它里面我从一个组合框中获取一个文本。问题是当我将文本保存在表格中时,它包含一个回车符。在调试模式下,它显示如下。
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.
删除 '#$A' 的东西。但这并不能消除它。
Is there any other way to do this..
有没有其他方法可以做到这一点..
Thanks
谢谢
回答by Marjan Venema
Remove the quotes around the #$A:
删除#$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.
# 告诉 delphi 您正在通过其数字代码指定一个字符。$ 表示您以十六进制指定。A 是值。
With the quotes you are searching for the presence of the #$A characters in the string, which aren't found, so nothing is replaced.
使用引号,您正在搜索字符串中是否存在 #$A 字符,但未找到这些字符,因此不会替换任何内容。
回答by jcfaria
Adapted from http://www.delphipages.com/forum/showthread.php?t=195756
改编自http://www.delphipages.com/forum/showthread.php?t=195756
The '#' denotes an ASCII character followed by a byte value (0..255).
'#' 表示一个 ASCII 字符后跟一个字节值 (0..255)。
The $A
is hexadecimal which equals 10
and $D
is hexadecimal which equals 13
.
的$A
是十六进制,等于10
和$D
十六进制,等于13
。
#$A
and #$D
(or #10
and #13
) are ASCII line feed and carriage return characters respectively.
#$A
and #$D
(或#10
and #13
) 分别是 ASCII 换行符和回车符。
Line feed = ASCII character $A
(hex) or 10
(dec): #$A
or #10
换行符 = ASCII 字符$A
(十六进制)或10
(十进制):#$A
或#10
Carriage return = ASCII character $D
(hex) or 13
(dec): #$D
or #13
回车符 = ASCII 字符$D
(十六进制)或13
(十进制):#$D
或#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?
为什么 Pos() 找不到它们?
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.
这就是 Delphi 向您显示控制字符的方式,如果您要这样做,Pos(#13, MyString)
否则Pos(#10, MyString)
它将返回位置。