SoFunction
Updated on 2025-03-02

Summary of the difference between containsString and rangeOfString in iOS

Preface

containsString can only be found in iOS8. RangeOfString is used instead of iOS7 and iOS7. containsString functions are simple and single. However, rangeOfString function is relatively complex. It can traverse the entire string. There are many situations in the options. What is the difference between the two? Let’s take a look together below.

1. containsString

 //Find whether the string contains "iOS" //Applicable toios8system,existios7system下会崩溃
NSString*str = @“I am iOS Development Engineer”;
if([str containsString:@" iOS"]) {
NSLog(@"String contains "iOS"");
  }

2. rangeOfString

 //Find whether the string contains "iOS" //It is also applicable under ios7 systemNSRangerange = [strrangeOfString:@" iOS"];
if( != NSNotFound) {//There is @"iOS"NSLog(@"String contains "iOS"");
  }

Notice:

1. - (BOOL)containsString:(NSString *)str NS_AVAILABLE(10_10, 8_0);

2. However, after the application review was approved, I encountered an extremely big pit.

3. Most 4s or 5s users complain that they have crashed when opening the page using this method.

4. Then when I looked at the API, I suddenly became dumbfounded. On the 6 and 7 systems, this method will definitely crash because it cannot be found.

5. Last. A more reliable way to judge or use

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.