NSString问题 - rangeOfString方法 [英] NSString question - rangeOfString method
问题描述
我遇到了一个我无法弄清楚的问题。
I am having an issue that I can't figure out.
我正在尝试对字符串运行rangeOfString方法,我不确定如何确定是否找不到该字符串。例如:
I'm trying to run the rangeOfString method on a string, and I'm not sure how to determine if the string was not found. For example:
NSRange range = [@"abc" rangeOfString:@"d" options:NSCaseInsensitiveSearch range:NSMakeRange(0,3)];
显然,d不包含在字符串abc中。我希望能够这样做:
Clearly, "d" is not contained in the string "abc." I'd like to be able to do this:
if(the range is empty since "d" is not in "abc")
//do something
这是什么代码?
谢谢!
推荐答案
来自的文档NSString
-[NSString rangeOfString]
返回值
Return Value
NSRange结构给出
的位置和长度
的接收者第一次出现aString。
如果aString未找到
或为空(@),则返回 {NSNotFound,0} 。
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批准了吗方式。
Is the Apple-approved way.
编辑:
我做了一个非常糟糕的拼写错误,修好了它,谢谢Kalle。
I made a really bad typo, fixed it, thanks Kalle.
这篇关于NSString问题 - rangeOfString方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!