Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if an NSString contains...?

People also ask

How do I check if a string contains a character in Objective C?

To check if a string contains another string in objective-c, we can use the rangeOfString: instance method where it returns the {NSNotFound, 0} if a 'searchString' is not found or empty (""). Output: string contains you!

What is the difference between string and NSString?

NSString is class and String is struct , I understand but NSString is an reference type ,how it is working inside struct.

What does NSString mean?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

Do I need to release NSString?

If you create an object using a method that begins with init, new, copy, or mutableCopy, then you own that object and are responsible for releasing it (or autoreleasing it) when you're done with it. If you create an object using any other method, that object is autoreleased, and you don't need to release it.


Here's how I would do it:

NSString *someString = @"Here is my string";
NSRange isRange = [someString rangeOfString:@"is " options:NSCaseInsensitiveSearch];
if(isRange.location == 0) {
   //found it...
} else {
   NSRange isSpacedRange = [someString rangeOfString:@" is " options:NSCaseInsensitiveSearch];
   if(isSpacedRange.location != NSNotFound) {
      //found it...
   }
}

You can easily add this as a category onto NSString:

@interface NSString (JRStringAdditions) 

- (BOOL)containsString:(NSString *)string;
- (BOOL)containsString:(NSString *)string
               options:(NSStringCompareOptions)options;

@end

@implementation NSString (JRStringAdditions) 

- (BOOL)containsString:(NSString *)string
               options:(NSStringCompareOptions)options {
   NSRange rng = [self rangeOfString:string options:options];
   return rng.location != NSNotFound;
}

- (BOOL)containsString:(NSString *)string {
   return [self containsString:string options:0];
}

@end

Use the following code to scan the word in sentence.

NSString *sentence = @"The quick brown fox";
NSString *word = @"quack";
if ([sentence rangeOfString:word].location != NSNotFound) {
    NSLog(@"Yes it does contain that word");
}

In iOS8 you can now use:

BOOL containsString = [@"Here is my string." containsString:@"is"];

There's an interesting post on how to "retrofit" it to iOS7 here: http://petersteinberger.com/blog/2014/retrofitting-containsstring-on-ios-7/


I recommend using NSLinguisticTagger. We can use it to search Here is my string. His isn't a mississippi isthmus. It is?

NSLinguisticTagger *linguisticTagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[
                                        NSLinguisticTagSchemeTokenType,
                                        ]
                                                                              options:
                                        NSLinguisticTaggerOmitPunctuation |
                                        NSLinguisticTaggerOmitWhitespace |
                                        NSLinguisticTaggerOmitOther ];
[linguisticTagger setString:@"Here is my string. His isn't a mississippi isthmus. It is?"];
[linguisticTagger enumerateTagsInRange:NSMakeRange(0,
                                                   [[linguisticTagger string] length])
                                scheme:NSLinguisticTagSchemeTokenType
                               options:
 NSLinguisticTaggerOmitPunctuation |
 NSLinguisticTaggerOmitWhitespace |
 NSLinguisticTaggerOmitOther |
 NSLinguisticTaggerJoinNames
                            usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                                NSLog(@"tag: %@, tokenRange: %@, sentenceRange: %@, token: %@",
                                      tag,
                                      NSStringFromRange(tokenRange),
                                      NSStringFromRange(sentenceRange),
                                      [[linguisticTagger string] substringWithRange:tokenRange]);
                            }];

This outputs:

tag: Word, tokenRange: {0, 4}, sentenceRange: {0, 19}, token: Here
tag: Word, tokenRange: {5, 2}, sentenceRange: {0, 19}, token: is
tag: Word, tokenRange: {8, 2}, sentenceRange: {0, 19}, token: my
tag: Word, tokenRange: {11, 6}, sentenceRange: {0, 19}, token: string
tag: Word, tokenRange: {19, 3}, sentenceRange: {19, 33}, token: His
tag: Word, tokenRange: {23, 2}, sentenceRange: {19, 33}, token: is
tag: Word, tokenRange: {25, 3}, sentenceRange: {19, 33}, token: n't
tag: Word, tokenRange: {29, 1}, sentenceRange: {19, 33}, token: a
tag: Word, tokenRange: {31, 11}, sentenceRange: {19, 33}, token: mississippi
tag: Word, tokenRange: {43, 7}, sentenceRange: {19, 33}, token: isthmus
tag: Word, tokenRange: {52, 2}, sentenceRange: {52, 6}, token: It
tag: Word, tokenRange: {55, 2}, sentenceRange: {52, 6}, token: is

It ignores His mississippi and isthmus and even identifies is inside of isn't.