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

2024-09-20 04:15:35 发布

您现在位置:Python中文网/ 问答频道 /正文

Python有非常有用的string.startswith()string.endswith()函数。 我可以使用哪些NSString方法来拥有相同的函数?


Tags: 方法函数stringstartswithendswithnsstring
3条回答

使用^{}^{}

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

您需要^{}^{}消息。

我倾向于经常使用^{}消息来实现相同的但不区分大小写的比较。

-hasPrefix()和-hasuffix()返回YES或NO,这取决于接收器是以给定的子字符串开始还是结束。如果startswith()和endswith()就是这样做的,那么这就是您的答案。

相关问题 更多 >