问题描述
我正在创建一个将输入字符串发送到移动设备的应用程序。某些设备在编码特殊字符时遇到问题,所以我想创建一个不允许PC上的用户输入特殊字符的过滤器。
I am creating an application which will send the input string to mobile device. Some devices have problems with encoding special characters so I would like to create a filter which doesn't allow the user on PC to enter special characters.
应用程序是以C#(.NET 3.5),我想附加一个按键事件的方法。伪代码如下:
The application is written in C# (.NET 3.5) and I would like to attach a method to key press event. The pseudocode is the following:
private void checkTextBoxContent(TextBox txtEntry)
{
if(txtEntry.Text contains non-ASCII sign)
{
show messageBox;
remove the last entered character;
}
}
有没有人知道是否有任何现有的检测方法ASCII /非ASCII符号,以便可以在条件
Does anyone know if there is any existing method that detects ASCII/non-ASCII sign so that could be used in condition
谢谢!
推荐答案
public static bool IsAllAscii(string text)
{
return text.All(c => c >= ' ' && c <= '~');
}
我不知道你真的想删除最后输入的字符虽然 - 考虑剪切和粘贴整个非ASCII字符串...
I'm not sure you would really want to just remove the last character entered though - consider cutting and pasting a whole non-ascii string...
这篇关于字符串过滤器:检测非ASCII符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!