Check Objective-C String for specific characters

MacBoss123541 picture MacBoss123541 · Oct 13, 2013 · Viewed 8k times · Source

For an app I'm working on, I need to check if a text field contains only the letters A, T, C, or G. Furthermore, I would like to make specialized error messages for any other inputed characters. ex) "Don't put in spaces." or "The letter b isn't an accepted value." I have read a couple other posts like this, but they are alphanumeric, I only want specified characters.

Answer

CRD picture CRD · Oct 13, 2013

One approach for you, far from unique:

NString has methods to find substrings, represented as an NSRange of location & offset, made up from characters in a given NSCharacterSet.

The set of what should be in the string:

NSCharacterSet *ATCG = [NSCharacterSet characterSetWithCharactersInString:@"ATCG"];

And the set of what shouldn't:

NSCharacterSet *invalidChars = [ATCG invertedSet];

You can now search for any range of characters consisting of invalidChars:

NSString *target; // the string you wish to check
NSRange searchRange = NSMakeRange(0, target.length); // search the whole string
NSRange foundRange = [target rangeOfCharacterFromSet:invalidChars
                                             options:0 // look in docs for other possible values
                                               range:searchRange];

If there are no invalid characters then foundRange.location will be equal to NSNotFound, otherwise you change examine the range of characters in foundRange and produce your specialised error messages.

You repeat the process, updating searchRange based on foundRange, to find all the runs of invalid characters.

You could accumulate the found invalid characters into a set (maybe NSMutableSet) and produce the error messages at the end.

You can also use regular expressions, see NSRegularExpressions.

Etc. HTH

Addendum

There is a really simple way to address this, but I did not give it as the letters you give suggest to me you may be dealing with very long strings and using provided methods as above may be a worthwhile win. However on second thoughts after your comment maybe I should include it:

NSString *target; // the string you wish to check
NSUInteger length = target.length; // number of characters
BOOL foundInvalidCharacter = NO;   // set in the loop if there is an invalid char

for(NSUInteger ix = 0; ix < length; ix++)
{
   unichar nextChar = [target characterAtIndex:ix]; // get the next character

   switch (nextChar)
   {
      case 'A':
      case 'C':
      case 'G':
      case 'T':
         // character is valid - skip
         break;

      default:
         // character is invalid
         // produce error message, the character 'nextChar' at index 'ix' is invalid
         // record you've found an error
         foundInvalidCharacter = YES;
   }
}

// test foundInvalidCharacter and proceed based on it

HTH