String contains numbers
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Program { static void Main(string[] args) { //this section create string variables. string stringVal = "this is a sample string without number"; string stringWithNumber = "this is a sample 3 string 7 with number 5"; Console.WriteLine("string.................."); Console.WriteLine("stringVal: " + stringVal); Console.WriteLine("stringWithNumber: " + stringWithNumber); //this line check string contains any digit/numeric value/number. Boolean result = stringVal.Any(char.IsDigit); //this line check string contains any digit/numeric value/number. Boolean result2 = stringWithNumber.Any(char.IsDigit); Console.WriteLine("\nstringVal contains any number (digit, numeric value)?........."); Console.WriteLine(result); Console.WriteLine("\nstringWithNumber contains any number (digit, numeric value)?..........."); Console.WriteLine(result2); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
string.................. stringVal: this is a sample string without number stringWithNumber: this is a sample 3 string 7 with number 5 stringVal contains any number (digit, numeric value)?......... False stringWithNumber contains any number (digit, numeric value)?........... True |