开发者

Check if a string has at least one number in it using LINQ

I would like to know what the easiest and shortest LINQ query is to return true if a string contains any number character 开发者_开发技巧in it.


"abc3def".Any(c => char.IsDigit(c));

Update: as @Cipher pointed out, it can actually be made even shorter:

"abc3def".Any(char.IsDigit);


Try this

public static bool HasNumber(this string input) {
  return input.Where(x => Char.IsDigit(x)).Any();
}

Usage

string x = GetTheString();
if ( x.HasNumber() ) {
  ...
}


or possible using Regex:

string input = "123 find if this has a number";
bool containsNum = Regex.IsMatch(input, @"\d");
if (containsNum)
{
 //Do Something
}


How about this:

bool test = System.Text.RegularExpressions.Regex.IsMatch(test, @"\d");


string number = fn_txt.Text;   //textbox
        Regex regex2 = new Regex(@"\d");   //check  number 
        Match match2 = regex2.Match(number);
        if (match2.Success)    // if found number 
        {  **// do what you want here** 
            fn_warm.Visible = true;    // visible warm lable
            fn_warm.Text = "write your text here ";   /
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜