字符串过滤器:检测非ASCII符号 [英] string filter: detect non-ASCII signs

查看:387
本文介绍了字符串过滤器:检测非ASCII符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个将输入字符串发送到移动设备的应用程序。某些设备在编码特殊字符时遇到问题,所以我想创建一个不允许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


txtEntry.Text中包含非ASCII符号

txtEntry.Text contains non-ASCII sign?

谢谢!

推荐答案

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符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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