Author Topic: How to test if one string contains another string  (Read 2031 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
How to test if one string contains another string
« on: August 20, 2014, 10:36:18 AM »
To test if one string contains another string, you can use the rangeOfString: method.

In Swift:

Code: [Select]
let string = "hello world"
if string.rangeOfString("hello") != nil {
   println("String found!")
}

Objective-C:

Code: [Select]
NSString *string = @"hello world";
if ([string rangeOfString:@"hello"].location == NSNotFound) {
  NSLog(@"string does not contain hello");
} else {
  NSLog(@"string contains hello!");
}

All the best!
Kevin McNeish
Author of "Learn to Code in Swift": https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
« Last Edit: August 14, 2015, 05:27:10 PM by kjmcneish »