First, the interception of NSString
1. Intercept from the beginning of the string to the specified position (not including the location)
NSString * string = @ "ABCdef";
NSString * subString1 = [string substringtoindex:3];
NSLog (@ "%@", subString1);
Output ABC
2. Starting at the end of the string specified position (including the position)
NSString * string = @ "ABCdef";
NSString * subString2 = [string substringfromindex:3];
NSLog (@ "%@", subString2);
Output def
Note: The first digit of the string has an index value of 0;
Second, the splicing of NSString
There are three ways to stitch a string
1.stringWithFormat
NSString * string1 = @ "123";
NSString * string2 = @ "456";
NSString * string3 = [NSString stringwithformat:@ "%@%@", string1, string2];
NSLog (@ "%@", string3);
Output 123456
2.stringByAppendingString
NSString * String4 = [string1 stringbyappendingstring:string2];
NSLog (@ "%@", String4);
Output 123456
3.stringByAppendingFormat
NSString * string5 = [string1 stringbyappendingformat:@ "%@", string2];
NSLog (@ "%@", string5);
Output 123456
Third, the NSString query
NSString * string = @ "ABCdef";
NSString * str = @ "abc";
1. Whether the query starts with a string
BOOL isornot = [string hasprefix:str];
2. Whether the query ends with a string
BOOL isornot = [string hassuffix:str];
3. Whether the query contains a string
Nsrange range = [string rangeofstring:str];
NSLog (@ "%ld,%ld", Range.location, Range.length); //location is where the query string is located, length is the size of the query string
Output 0, 3
Iv. Division of NSString
NSString * string = @ "Abc|def";
The Split method returns an array
Nsarray * arr = [string componentsseparatedbystring:@ "|"];
NSLog (@ "%@", arr);
Output (ABC, DEF)
Note: Little brother just opened a blog to write some essays, there is no place to hope that everyone in time, thank you.
NSString interception, stitching, querying, and splitting in iOS