Checking for multiple characters in nsstring

后端 未结 3 1852
广开言路
广开言路 2021-01-15 06:12

i have a string and i want to check for the multiple characters in this string the following code i working fine for one character how to check for the multiple characters.

3条回答
  •  无人及你
    2021-01-15 06:46

    UPDATE: The previous example was broken, as NSScanner should not be used like that. Here's a much more straight-forward example:

    NSString* string = @"ABCCDEDRFFED";
    NSCharacterSet* characters = [NSCharacterSet characterSetWithCharactersInString:@"ABC"];
    NSUInteger characterCount;
    
    NSUInteger i;
    for (i = 0; i < [yourString length]; i++) {
      unichar character = [yourString characterAtIndex:i];
      if ([characters characterIsMember:character]) characterCount++;
    }
    
    NSLog(@"Total characters = %d", characterCount);
    

提交回复
热议问题