I'm trying to detect if a string contains any characters outside of a range of values (ASCII), from ,(comma) to |(pipe). I tried this and it didnt work
string.Contains("[^,-|]");
any ideas?
I'm trying to detect if a string contains any characters outside of a range of values (ASCII), from ,(comma) to |(pipe). I tried this and it didnt work
string.Contains("[^,-|]");
any ideas?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
rocketPosted Jan 14, 2008, 3:50 PM
Guest UserPosted Jan 14, 2008, 3:30 PM
IndexOfAny() should work, but since it takes a char array instead of a string, you'll need to do an extra function call, like this:
string textToEvaluate = "ABCDEF[GHIJ";
string badChars = "[^,-|]";
int i = textToEvaluate.IndexOfAny(badChars.ToCharArray());
If none of the bad chars are in the string to be evaluated, i = -1, otherwise it will contain the position of the first bad char.
HTH