How to find whether a string contains any of the special characters?
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 characte开发者_开发知识库rs in the string?
Use String.IndexOfAny
:
private static readonly char[] SpecialChars = "!@#$%^&*()".ToCharArray();
...
int indexOf = text.IndexOfAny(SpecialChars);
if (indexOf == -1)
{
// No special chars
}
Of course that will loop internally - but at least you don't have to do it in your code.
Regex RgxUrl = new Regex("[^a-z0-9]");
blnContainsSpecialCharacters = RgxUrl.IsMatch(stringToCheck);
Instead of checking if a string contains "special characters" it is often better to check that all the characters in the string are "ordinary" characters, in other words use a whitelist instead of a blacklist. The reason is that there are a lot of characters that could be considered "special characters" and if you try to list them all you are bound to miss one.
So instead of doing what you asked it might be better to check for example that your string matches the regex @"^\w+$"
, or whatever you need.
Linq is the new black.
string.Any(c => char.IsSymbol(c));
For IsSymbol(), valid symbols are members of UnicodeCategory.
- Letterlike symbols, which include a set of mathematical alphanumeric symbols as well as symbols such as ℅, №, and ™
- Number forms, such as subscripts and superscripts
- Mathematical operators and arrows
- Geometric symbols
- Technical symbols
- Braille patterns
- Dingbats
Edit:
This does not catch ALL characters. This may supplement:
string.Any(c => !char.IsLetterOrDigit(c));
Here is a short solution to check for special chars using LINQ
private bool ContainsSpecialChars(string value)
{
var list = new[] {"~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "\""};
return list.Any(value.Contains);
}
I was trying to accomplish a different result. To create a method which returns the special character used to separate a determined string, say 1#4#5, to then use it to Split that same string. Since the sistem I am maintaining is built by so many different people, and in some cases, I have no idea which is the pattern(Usually there's none), the following helped me a lot. And I cant vote up as yet.
public String Separator(){
**String specialCharacters = "%!@#$#^&*()?/>.<:;\\|}]{[_~`+=-" +"\"";**
char[] specialCharactersArray = specialCharacters.toCharArray();
char[] a = entrada.toCharArray();
for(int i = 0; i<a.length; i++){
for(int y=0; y < specialCharactersArray.length; i++){
if(a[i] == specialCharactersArray[y]){
separator = String.valueOf(specialCharactersArray[y]);
}
}
}
return separator;
}
Thank you guys.
//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;
}
Also...
foreach (char character in "some*!@#@!#string")
{
if(!Char.IsLetterOrDigit(character))
{
//It is a special character.
}
}
def splchar(str):
splchar=("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=")
for chr in splchar:
if (chr in str):
print("not accepted")
break
else:
print("accepted")
str="GeeksForGeeks"
splchar(str)
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.
精彩评论