C# 检查字符串是否有无效字符?最聪明的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12350801/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:58:52  来源:igfitidea点击:

Check string for invalid characters? Smartest way?

c#.netlistchar

提问by silla

I would like to check some string for invalid characters. With invalid characters I mean characters that should not be there. What characters are these? This is different, but I think thats not that importan, important is how should I do that and what is the easiest and best way (performance) to do that?

我想检查一些字符串是否有无效字符。对于无效字符,我的意思是不应该存在的字符。这些是什么字符?这是不同的,但我认为那不是那么重要,重要的是我应该如何做到这一点以及最简单和最好的方法(性能)是什么?

Let say I just want strings that contains 'A-Z', 'empty', '.', '$', '0-9'

假设我只想要包含“A-Z”、“空”、“.”、“$”、“0-9”的字符串

So if i have a string like "HELLO STaCKOVERFLOW" => invalid, because of the 'a'. Ok now how to do that? I could make a List<char>and put every char in it that is not allowed and check the string with this list. Maybe not a good idea, because there a lot of chars then. But I could make a list that contains all of the allowed chars right? And then? For every char in the string I have to compare the List<char>? Any smart code for this? And another question: if I would add A-Z to the List<char>I have to add 25 chars manually, but these chars are as I know 65-90 in the ASCII Table, can I add them easier? Any suggestions? Thank you

所以如果我有一个像“ HELLO STaCKOVERFLOW”这样的字符串=>无效,因为'a'。好的,现在该怎么做?我可以制作一个List<char>并将每个不允许的字符放入其中并使用此列表检查字符串。也许不是一个好主意,因为那时有很多字符。但是我可以制作一个包含所有允许字符的列表,对吗?进而?对于字符串中的每个字符,我必须比较List<char>? 任何智能代码?另一个问题:如果我将 AZ 添加到List<char>我必须手动添加 25 个字符,但这些字符是我所知道的 ASCII 表中的 65-90,我可以更容易地添加它们吗?有什么建议?谢谢

采纳答案by ThiefMaster

You can use a regular expression for this:

您可以为此使用正则表达式:

Regex r = new Regex("[^A-Z0-9.$ ]$");
if (r.IsMatch(SomeString)) {
    // validation failed
}


To create a list of characters from A-Zor 0-9you would use a simple loop:

要从A-Z0-9使用简单的循环创建字符列表:

for (char c = 'A'; c <= 'Z'; c++) {
    // c or c.ToString() depending on what you need
}

But you don't need that with the Regex - pretty much every regex engine understands the range syntax (A-Z).

但是您不需要使用正则表达式 - 几乎每个正则表达式引擎都理解范围语法 ( A-Z)。

回答by FrankieTheSkin

I have only just written such a function, and an extended version to restrict the first and last characters when needed. The original function merely checks whether or not the string consists of valid characters only, the extended function adds two integers for the numbers of valid characters at the beginning of the list to be skipped when checking the first and last characters, in practice it simply calls the original function 3 times, in the example below it ensures that the string begins with a letter and doesn't end with an underscore.

我刚刚写了一个这样的函数,还有一个扩展版本,用于在需要时限制第一个和最后一个字符。原函数只检查字符串是否只由有效字符组成,扩展函数在检查首尾字符时将列表开头的有效字符数加两个整数,在实际中它只是调用原始函数 3 次,在下面的示例中,它确保字符串以字母开头而不以下划线结尾。

StrChr(String, "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
StrChrEx(String, "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 11, 1));


BOOL __cdecl StrChr(CHAR* str, CHAR* chars)
{
 for (int s = 0; str[s] != 0; s++)
 {
     int c = 0;

    while (true)
    {
        if (chars[c] == 0)
        {
             return false;
        }
         else if (str[s] == chars[c])
         {
            break;
         }
        else
         {
            c++;
         }
     }
 }

return true;
}

BOOL __cdecl StrChrEx(CHAR* str, CHAR* chars, UINT excl_first, UINT excl_last)
{
char first[2]   = {str[0], 0};
char last[2]    = {str[strlen(str) - 1], 0};

if (!StrChr(str, chars))
{
    return false;
}

if (excl_first != 0)
{
    if (!StrChr(first, chars + excl_first))
    {
        return false;
    }
}

if (excl_last != 0)
{
    if (!StrChr(last, chars + excl_last))
    {
        return false;
    }
}

return true;
}

回答by user3417173

If you are using c#, you do this easily using List and contains. You can do this with single characters (in a string) or a multicharacter string just the same

如果您使用的是 c#,则可以使用 List 和 contains 轻松完成此操作。您可以使用单个字符(在字符串中)或相同的多字符字符串执行此操作

  var pn = "The String To ChecK";      
  var badStrings = new List<string>()
  {
  " ","\t","\n","\r"
  };
  foreach(var badString in badStrings)
  {
    if(pn.Contains(badString))
    {
     //Do something
    }
  }