String contains special characters
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 30 |
class Program { static void Main(string[] args) { //this section create string variables. string colors = "1 Red 2 green 3 blue"; string colors2 = "#red #green #blue"; string colors3 = "red* green* blue*"; Console.WriteLine("string.................."); Console.WriteLine("colors: " + colors); Console.WriteLine("colors2: " + colors2); Console.WriteLine("colors3: " + colors3); //check string contains any special characters without space Regex rex = new Regex("^[a-z0-9 ]+$", RegexOptions.IgnoreCase); Boolean result = rex.IsMatch(colors); Boolean result2 = rex.IsMatch(colors2); Boolean result3 = rex.IsMatch(colors3); Console.WriteLine("colors contains no special characters without space? " + result); Console.WriteLine("colors2 contains no special characters without space? " + result2); Console.WriteLine("colors3 contains no special characters without space? " + result3); Console.ReadLine(); } } |
Ouput:
1 2 3 4 5 6 7 8 9 |
string.................. colors: 1 Red 2 green 3 blue colors2: #red #green #blue colors3: red* green* blue* colors contains no special characters without space? True colors2 contains no special characters without space? False colors3 contains no special characters without space? False |