Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an opposite of IndexOf()? Perhaps NotIndexOf()?

Tags:

c#

If myStr is supposed to contain 0's and 1's, how can I search this string for anything that is not a 0 or 1?

For example:

string myStr = "1001g101";

if (myStr.IndexOf(NOT "0") != -1 && myStr.IndexOf(NOT "1") != -1) {
    Console.Write("String contains something that isn't 0 or 1!");
}

My reason for doing this is not wanting to do a full ASCII character map and have it check each character against all ASCII characters, it seems way too inefficient. If I have to check each character and ensure 0 or 1, that will work, but is there a better way?

I am not good with Regex, but I suspect that may hold my answer.

like image 246
Taurophylax Avatar asked Dec 11 '22 09:12

Taurophylax


1 Answers

Or use LINQ:

if (myStr.Any(c => c != '0' && c != '1'))
    ....
like image 72
Michael Gunter Avatar answered Dec 13 '22 21:12

Michael Gunter