Click here to Skip to main content
14,665,550 members
Rate this:
Please Sign up or sign in to vote.
See more:
how can check weather a string Contain numbers special Char etc
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 1

IndexOfAny()[^] looks like a good choice.
   
Comments
VJ Reddy 16-Apr-12 11:21am
   
Good link. 5!
Sergey Alexandrovich Kryukov 16-Apr-12 22:29pm
   
Agree, a 5.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

The Solution 1 given by Richard MacCutchan is very good.
As an alternative if you want to check whether the string contains a particular category of characters say Punctuation or Numbers etc., then the System.Text.RegularExpressions.Regex class can be considered to test as shown below:
string text = "Is this text, contains special chars?";
//The Regex pattern matches if atleast one character of the
//alternatives is found \p{P} Unicode punctuation category
//\p{N} Unicode numbers category
if(System.Text.RegularExpressions.Regex.IsMatch(text,@"\p{P}|\p{N}"))
    Console.WriteLine ("Contains special chars");
else
    Console.WriteLine ("Does not contain special chars");

//Output
//Contains special chars
   
v2
Comments
Sergey Alexandrovich Kryukov 16-Apr-12 22:29pm
   
A valid alternative, a 5.
--SA
VJ Reddy 16-Apr-12 23:31pm
   
Thank you very much, SA.
Rate this:
Please Sign up or sign in to vote.

Solution 3

You might try below,

class Program
{
    static void Main(string[] args)
    {
        string randomCharacters = "HelloWorld2090!!!@#$";
        var result = randomCharacters.Any(ch => Char.IsNumber(ch) || Char.IsPunctuation(ch) || Char.IsSymbol(ch));
        Console.WriteLine(result);
    }
}


Hope it helps :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100