Check string for invalid characters? The smartest way?

I want to check some string for invalid characters. With invalid characters, I mean characters that shouldn't be. What kind of characters are these? It is different, but I think it is not so important, it is important how I should do it, and what is the easiest and best way (performance) for this?

Say I just need strings containing 'A-Z', 'empty', '.', '$', '0-9'

So, if I have a string like " HELLO STACKOVERFLOW " => is invalid, due to 'a'. Ok, how to do this? I could make a List<char> and put in it all char that are not allowed, and check the line with this list. Maybe this is not a good idea, because there are a lot of characters. But could I make a list containing all valid characters? And then? For each char in a string, do I need to compare List<char> ? Any smart code for this? And one more question: if I added AZ to the List<char> , I need to add 25 characters manually, but these characters, as I know, are 65-90 in the ASCII table, can I add them more easily? Any suggestions? Thanks you

+7
source share
2 answers

To do this, you can use the regular expression:

 Regex r = new Regex("[^A-Z0-9.$ ]$"); if (r.IsMatch(SomeString)) { // validation failed } 

To create a list of characters from AZ or 0-9 , you should use a simple loop:

 for (char c = 'A'; c <= 'Z'; c++) { // c or c.ToString() depending on what you need } 

But you don't need this with Regex - almost every regex engine understands range syntax ( AZ ).

+15
source

I just wrote such a function and an extended version to limit the first and last characters when necessary. The original function simply checks if only a string consists of only valid characters, the extended function adds two integers for the number of valid characters at the beginning of the list that you need to skip when checking the first and last characters, in practice it just calls the original function 3 times, in the example below, it ensures that the string begins with a letter and does not end with an underscore.

 StrChr(String, "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")); StrChrEx(String, "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 11, 1)); BOOL __cdecl StrChr(CHAR* str, CHAR* chars) { for (int s = 0; str[s] != 0; s++) { int c = 0; while (true) { if (chars[c] == 0) { return false; } else if (str[s] == chars[c]) { break; } else { c++; } } } return true; } BOOL __cdecl StrChrEx(CHAR* str, CHAR* chars, UINT excl_first, UINT excl_last) { char first[2] = {str[0], 0}; char last[2] = {str[strlen(str) - 1], 0}; if (!StrChr(str, chars)) { return false; } if (excl_first != 0) { if (!StrChr(first, chars + excl_first)) { return false; } } if (excl_last != 0) { if (!StrChr(last, chars + excl_last)) { return false; } } return true; } 
0
source

All Articles