简体   繁体   English

检查字符串是否包含忽略大小写的字符

[英]Check if a string contains a character ignoring case

I have a textbox on GUI which takes an "Mobile Number" as input. 我在GUI上有一个文本框,它使用“手机号码”作为输入。 I want to validate it to find out if it has some characters, which would mean the number is invalid 我想验证它是否有一些字符,这意味着数字无效

So, a number 9876543210 is valid while a number 98765df013 is invalid 因此,数字9876543210 有效,而数字98765df013 无效

I made a array of all characters (which are not allowed by me) 我对所有字符进行了排列(这是我不允许的)

string[] alphabeticChars       = new string[] {    
                                                                    "a", "b", "c", "d", "e", "f", "g",
                                                                    "h", "i", "j", "k", "l", "m", "n",
                                                                    "o", "p", "q", "r", "s", "t", "u",
                                                                    "v", "w", "x", "y", "z"
                                                               };

and then I wrote a validation function 然后我写了一个验证函数

public bool HasCharacters(string text)
{
    foreach(string character in this.alphabeticChars)
         if(text.Contains(character.ToLower()) || text.Contains(character.ToUpper())) 
                return true;

    return false;
}

As you can see, I need to call Contains twice, one for "Lower Case" and another time for "Upper Case". 如您所见,我需要调用Contains两次,一次是“ Lower Case”,另一次是“ Upper Case”。 I checked and couldn't find ContainsIgnoreCase or something. 我检查了一下,找不到ContainsIgnoreCase之类的东西。

What is the best way to to such a thing ? 做到这种事情的最好方法是什么? (Don't mention regular expressions, as I don't want to use them) (不要提及正则表达式,因为我不想使用它们)

In a very simple way :-) 以非常简单的方式:-)

foreach(string character in this.alphabeticChars)
     if(text.ToLower().Contains(character)) 
            return true;

Or maybe you can do a regular expression which is more efficient 或者也许您可以做一个更有效的正则表达式

My answer it is not exactly what you are asking, it more suggestion, better way to validate phone numbers is using of libphonenumber (c#) library https://libphonenumber.codeplex.com that is port of Google libphonenumber. 我的回答不完全是您要问的,它是更多建议,一种更好的验证电话号码的方法是使用libphonenumber(c#)库https://libphonenumber.codeplex.com ,它是Google libphonenumber的端口。 I have found it as the best way to do it for me. 我发现它是为我做的最好方法。 You can validate numbers based on country code and to format them in output. 您可以根据国家/地区代码验证数字并在输出中设置其格式。

Update. 更新。 Here is more up to date version of the library https://www.nuget.org/packages/libphonenumber-csharp/ 这是该库的最新版本https://www.nuget.org/packages/libphonenumber-csharp/

尝试这个:

bool isValid = !text.Any(c => Char.IsLetter(c));

My take, using Linq: 我的看法,使用Linq:

public static class StringExtensions
{
    public static bool ContainsOnlyDigits(this string s)
    {
        return s.All(c => c >= '0' && c <= '9');
    }
}

And in use: 并在使用中:

var result = "12345678".ContainsOnlyDigits();

由于您似乎想测试数字(因为您的预期输入是“手机号码”,因此它可能是单行代码:

bool isValid = text.All(Char.IsDigit);

Instead of checking the presence of a character, switch that logic around and just verify that everything is a number: 无需检查字符是否存在,而是切换逻辑并仅验证所有内容都是数字:

public bool IsNumeric(string text)
{
    return text.All(char.IsDigit);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM