Objective-C (cocoa) equivalent to python's endswith/beginswith
Python has string.startswi开发者_如何学Goth()
and string.endswith()
functions which are pretty useful.
What NSString methods can I use to have the same function?
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.
精彩评论