开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜