Position of a Substring in NSString

ios, ios4, iphone, objective-c, xcode

Solution

`2147483647` is the same thing as `NSNotFound`, which means the string you searched for (`searchKeyword`) wasn't found.

NSRange range = [string rangeOfString:searchKeyword];
if (range.location == NSNotFound) {
    NSLog(@"string was not found");
} else {
    NSLog(@"position %lu", (unsigned long)range.location);
}

Problem

How I can get the position/Index of a substring within an `NSString`? I am finding the location in the following way. ``` NSRange range = [string rangeOfString:searchKeyword]; NSLog (@"match found at index:%u", range.location); ``` This returns `index:2147483647` when `searchKeyword` is a substring within `string`. How i can get the index value like `20` or `5` like that?

Original source