NSString question - rangeOfString method

前端 未结 2 524
青春惊慌失措
青春惊慌失措 2020-12-14 15:14

I am having an issue that I can\'t figure out.

I\'m trying to run the rangeOfString method on a string, and I\'m not sure how to determine if the string was not foun

相关标签:
2条回答
  • 2020-12-14 15:59

    From the documentation of 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 (@"").

    So it looks like:

    if ([@"abc" rangeOfString:@"d"].location == NSNotFound){
      //Do something
    

    Is the Apple-approved way.

    EDIT:

    I made a really bad typo, fixed it, thanks Kalle.

    0 讨论(0)
  • 2020-12-14 16:13

    Check the length of the range. If it's non-zero, it was found.

    0 讨论(0)
提交回复
热议问题