Checking for multiple characters in nsstring

后端 未结 3 1849
广开言路
广开言路 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:44

    Also look up NSCountedSet. It can help you keep count of multiple instances of the same character.

    For example, from the docs:

    countForObject: Returns the count associated with a given object in the receiver.

    - (NSUInteger)countForObject:(id)anObject
    

    Parameters anObject The object for which to return the count.

    Return Value The count associated with anObject in the receiver, which can be thought of as the number of occurrences of anObject present in the receiver.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-15 06:48

    Have a look at the following method in NSCharacterSet:

    + (id)characterSetWithCharactersInString:(NSString *)aString
    

    You can create a character set with more than one character (hence the name character set), by using that class method to create your set. The parameter is a string, every character in that string will end up in the character set.

    0 讨论(0)
提交回复
热议问题