0

Hey sorry for the messy title, However I cannot really find the right definition for this group of sings (!@#$%^&*()_+?><), however, there is any way I would be able to check if my string contain any of those sings? I was really struggling, And honestly I was tryng to do it using a very messy "if" mentioned below:

if (c == '!' || c == '@' || c == '#' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '(' || c == ')' || c == '' || c == '' || c == '+' || c == '{' || c == '}' || c == '/' || c == '>' || c == '<' || c == '>' || c == '~' || c == '`' || c == '?' || c == ':' || c == '"')

Im sure there is a better way to check it, but I do not know any function that could check it. Help would be appreciated.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
lolshimi11
  • 37
  • 1
  • regex is your good friend – Maytham Fahmi Jun 04 '20 at 09:38
  • 6
    Does this answer your question? [Check for special characters (/\*-+\_@&$#%) in a string?](https://stackoverflow.com/questions/4503542/check-for-special-characters-in-a-string) – George Kerwood Jun 04 '20 at 09:39
  • 1
    What about other Unicode characters? Do you perhaps actually want a whitelist? – Daniel B Jun 04 '20 at 09:39
  • If you don't want to use regex, you can use `IndexOfAny`. Something like: `c.IndexOfAny("!@#$%^&*()_+?><".ToCharArray()) != -1` – Joelius Jun 04 '20 at 09:43
  • 1
    it is a lot that you are looking for. What is the complementary set? meaning the alternative? only letters and numbers? maybe it is easier to check for those ?=! – Mong Zhu Jun 04 '20 at 09:59

1 Answers1

-1

You could probably use Regex.

// text here refers to your entire string
if (Regex.Matches(text, @"[!@#$%^&*()_+?><]"))
{
    // do something
}

Something similar to that should do the trick.

TheForgot3n1
  • 212
  • 2
  • 11
  • Sorry for my lack of understanding,but 'c' inside the "Regex.Matches" reffers to a char "c"? And if so, it should be running inside "for" or "foreach" loop, right? – lolshimi11 Jun 04 '20 at 10:23
  • I beliveve my regex was actually wrong. Yes the c was supposed to refer to the string you were talking about, not each individual character. – TheForgot3n1 Jun 04 '20 at 22:55