I want to find whether a string contains any of the special characters like !,@,#,$,%,^,&,*,(,)....etc.
How can I do that without looping thorugh all the charact
Using PHP, you can try:
if (preg_match("[^A-Za-z0-9]", $yourString) {
//DO WHATEVER
}
This will return TRUE if the string contains anything other than a letter or number.
Hope this helps.
Regex RgxUrl = new Regex("[^a-z0-9]");
blnContainsSpecialCharacters = RgxUrl.IsMatch(stringToCheck);
//apart from regex we can also use this
string input = Console.ReadLine();
char[] a = input.ToCharArray();
char[] b = new char[a.Length];
int count = 0;
for (int i = 0; i < a.Length; i++)
{
if (!Char.IsLetterOrDigit(a[i]))
{
b[count] = a[i];
count++;
}
}
Array.Resize(ref b, count);
foreach(var items in b)
{
Console.WriteLine(items);
}
Console.ReadLine();
//it will display the special characters in the string input
public static bool HasSpecialCharacters(string str)
{
string specialCharacters = @"%!@#$%^&*()?/>.<,:;'\|}]{[_~`+=-" +"\"";
char[] specialCharactersArray = specialCharacters.ToCharArray();
int index = str.IndexOfAny(specialCharactersArray);
//index == -1 no special characters
if (index == -1)
return false;
else
return true;
}