delphi - 从字符串中删除所有非标准文本字符
我需要从字符串中删除所有非标准文本字符。我需要删除所有非 ASCII 字符和控制字符(换行符/回车符除外)。
I need to strip out all non standard text characers from a string. I need remove all non ascii and control characters (except line feeds/carriage returns).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是 Cosmin 的一个变体,它只遍历字符串一次,但使用高效的分配模式:
And here's a variant of Cosmin's that only walks the string once, but uses an efficient allocation pattern:
像这样的事情应该做:
对于
UnicodeString
你可以做类似的事情。Something like this should do:
For
UnicodeString
you can do something similar.如果您不需要就地执行此操作,而是生成字符串的副本,请尝试此代码
并按如下方式使用它
编辑:为 DEL ctrl 字符添加#127。
EDIT2:这是一个更快的版本,谢谢 ldsandon
if you don't need to do it in-place, but generating a copy of the string, try this code
and use it like this
EDIT: added #127 for DEL ctrl char.
EDIT2: this is a faster version, thanks ldsandon
这是一个版本,它不是通过逐个附加字符来构建字符串,而是一次性分配整个字符串。它需要检查字符串两次,一次是为了计算“好”字符,一次是为了有效地复制这些字符,但这是值得的,因为它不会进行多次重新分配:
Here's a version that doesn't build the string by appending char-by-char, but allocates the whole string in one go. It requires going over the string twice, once to count the "good" char, once to effectively copy those chars, but it's worth it because it doesn't do multiple reallocations:
我的性能解决方案;
我用delphi xe7写的
my performance solution;
I wrote by delphi xe7
我的版本带有字节结果数组:
及功能:
my version with Result array of byte :
and the function :