我遇到了一个无法解决的问题。
我正在尝试对字符串运行 rangeOfString 方法,但我不确定如何确定是否找不到该字符串。例如:
NSRange range = [@"abc" rangeOfString:@"d" options:NSCaseInsensitiveSearch range:NSMakeRange(0,3)];
显然,字符串“abc”中不包含“d”。我希望能够做到这一点:
if(the range is empty since "d" is not in "abc")
//do something
这个代码是什么?
谢谢!!
最佳答案
来自NSString的文档
-[NSString rangeOfString]
Return Value
An NSRange structure giving the location and length in the receiver of the first occurrence of aString. Returns {NSNotFound, 0} if aString is not found or is empty (@"").
所以看起来像:
if ([@"abc" rangeOfString:@"d"].location == NSNotFound){
//Do something
是 Apple 认可的方式。
编辑:
我犯了一个非常严重的拼写错误,已修复它,谢谢 Kalle。
关于iphone - NSString 问题 - rangeOfString 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3504420/