Objective-C(cocoa)相当于python的endswith/startswith

pro*_*eek 45 python cocoa objective-c

Python有string.startswith()string.endswith()功能,这是非常有用的.我可以使用哪些NSString方法来实现相同的功能?

Ada*_*eld 62

使用-hasPrefix:-hasSuffix::

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


Mac*_*Mac 8

你想要hasPrefixhasSuffix消息.

我倾向于也compare:options:经常使用该消息来实现相同的但不区分大小写的比较.


Cal*_*leb 5

-hasPrefix()和-hasSuffix()返回YES或NO,具体取决于接收者是以给定子字符串开头还是结尾.如果那是startswith()和endswith()那么,那就是你的答案.