开发者

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

开发者 https://www.devze.com 2023-02-15 10:58 出处:网络
Python has string.startswi开发者_如何学Goth() and string.endswith() functions which are pretty useful.

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

精彩评论

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

关注公众号