从字符串中删除隐藏的字符 [英] Removing hidden characters from within strings

查看:184
本文介绍了从字符串中删除隐藏的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:

我有一个.NET应用程序,可通过电子邮件发送新闻通讯。在Outlook中查看新闻通讯时,Outlook会显示一个问号代替它无法识别的隐藏字符。这些隐藏的字符来自最终用户,他们将构成新闻通讯的html复制并粘贴到表单中并提交。如果隐藏的字符出现在字符串的末尾或开头,则c#trim()会删除它们。使用gmail查看新闻简报时,gmail可以很好地忽略它们。将这些隐藏字符粘贴到Word文档中时,我打开了显示段落标记和隐藏符号选项,这些符号在较大的矩形内显示为一个矩形。另外,构成新闻通讯的文本可以是任何语言,因此必须接受Unicode字符。我尝试遍历字符串以检测字符,但是该循环无法识别该字符并将其传递。还要求最终用户先将html粘贴到记事本中,然后再提交。

I have a .NET application that sends out newsletters via email. When the newsletters are viewed in outlook, outlook displays a question mark in place of a hidden character it can’t recognize. These hidden character(s) are coming from end users who copy and paste html that makes up the newsletters into a form and submits it. A c# trim() removes these hidden chars if they occur at the end or beginning of the string. When the newsletter is viewed in gmail, gmail does a good job ignoring them. When pasting these hidden characters in a word document and I turn on the "show paragraph marks and hidden symbols" option the symbols appear as one rectangle inside a bigger rectangle. Also the text that makes up the newsletters can be in any language, so accepting Unicode chars is a must. I've tried looping through the string to detect the character but the loop doesn't recognize it and passes over it. Also asking the end user to paste the html into notepad first before submitting it is out of the question.

我的问题:

如何使用C#检测和消除这些隐藏字符?

My question:
How can I detect and eliminate these hidden characters using C#?

推荐答案

您可以使用以下命令从输入字符串中删除所有控制字符:

You can remove all control characters from your input string with something like this:

string input; // this is your input string
string output = new string(input.Where(c => !char.IsControl(c)).ToArray());

此处是 IsControl()方法的文档

Here is the documentation for the IsControl() method.

或者,如果您只想保留字母和数字,也可以使用 IsLetter IsDigit 函数:

Or if you want to keep letters and digits only, you can also use the IsLetter and IsDigit function:

string output = new string(input.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray());

这篇关于从字符串中删除隐藏的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆