String contains numbers only
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 stringValue = "this is a sample 5 string 7 with number"; string stringNumbers = "123456789502"; Console.WriteLine("string.................."); Console.WriteLine("stringValue: " + stringValue); Console.WriteLine("stringNumbers: " + stringNumbers); //this line check string contains only digit/numeric value/number or not. Boolean isNumeric = stringValue.All(char.IsDigit); //this line check string contains only digit/numeric value/number or not. Boolean isNumeric2 = stringNumbers.All(char.IsDigit); Console.WriteLine("\nstringValue contains only number (digit, numeric value)?........."); Console.WriteLine(isNumeric); Console.WriteLine("\nstringNumbers contains only number (digit, numeric value)?..........."); Console.WriteLine(isNumeric2); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 | string.................. stringValue: this is a sample 5 string 7 with number stringNumbers: 123456789502 stringValue contains only number (digit, numeric value)?......... False stringNumbers contains only number (digit, numeric value)?........... True |