Objective-C (cocoa) equivalent to python's endswith/beginswith Objective-C (cocoa) equivalent to python's endswith/beginswith python python

Objective-C (cocoa) equivalent to python's endswith/beginswith


Use -hasPrefix: and -hasSuffix::

NSString *s = @"foobar";NSLog(@"%d %d\n", [s hasPrefix:@"foo"], [s hasSuffix:@"bar"]);// Output: "1 1"


You want the hasPrefix and hasSuffix messages.

I tend to also use the compare:options: message pretty regularly to achieve the same but with case-insensitive comparison.


-hasPrefix() and -hasSuffix() return YES or NO depending on whether the receiver begins or ends with the given substring. If that's what startswith() and endswith() do, then that's your answer.