NSString, immutable strings, that is, content and length cannot be modified after creation.
Nsmutablestring, a mutable string that can be modified after it is created.
Immutable string (NSString)
Create a string
1 // create string
2 NSString * str1 = [[NSString alloc] initWithString: @ "QQ"];
3 NSString * str2 = [NSString stringWithString: @ "WeChat"];
4 // create string literally
5 // Literals are also syntactic sugar
6 NSString * str3 = @ "renren";
7 NSLog (@ "% @", str3);
Literals are typically used to create strings.
Other ways to String
1 // string length
2 NSLog (@ "length:% lu", str3.length);
3 // take the substring
4 NSString * str4 = @ "iPhone6Plus";
5 // fromIndex takes the current subscript to the end of the string, including the current subscript
6 NSString * str5 = [str4 substringFromIndex: 4];
7 NSLog (@ "% @", str5);
8 // toIndex takes from the beginning of the string to the end of a certain index, does not include the current index
9 NSString * str6 = [str4 substringToIndex: 4];
10 NSLog (@ "% @", str6);
11 // withRange
12 // Take the substring through the range structure
13 NSString * str7 = [str4 substringWithRange: NSMakeRange (3, 4)];
14 NSLog (@ "% @", str7);
15
16 // spliced strings can be nested
17 NSString * str8 = [str4 stringByAppendingString: @ "5288"];
18 NSLog (@ "% @", str8);
19
20 // Replacement strings can be used nested
21 NSString * str9 = [str8 stringByReplacingOccurrencesOfString: @ "6Plus" withString: @ "7s"];
22 NSLog (@ "% @", str9);
twenty three
24 // String equality judgment
25 NSString * str10 = @ "123";
26 NSString * str11 = @ "123";
27 if (str10 == str11) {
28 NSLog (@ "Two string objects have the same address");
29}
30 // if ([str10 isEqualToString: str11]) {
31 NSLog (@ "The contents of the two string objects are the same");
32}
33
34 // determine prefix and suffix
35 if ([str9 hasPrefix: @ "i"]) {
36 NSLog (@ "有 prefix");
37}
38 if ([str9 hasSuffix: @ "88"]) {
39 NSLog (@ "has suffix");
40}
41 if ([str9 hasPrefix: @ "iPhone7s5288"]) {
42 NSLog (@ "有 prefix");
43}
44 if ([str9 hasSuffix: @ "iPhone7s5288"]) {
45 NSLog (@ "has suffix");
46}
47
48 // string comparison
49 // ASC ascending DESC descending
50 NSString * str12 = @ "iPhone6";
51 NSString * str13 = @ "iPhone5s";
52 // Define a comparison result variable to store the comparison result of the string
53 NSComparisonResult result = [str12 compare: str13];
54 if (result == NSOrderedAscending) {
55 NSLog (@ "Ascending");
56} else if (result == NSOrderedDescending) {
57 NSLog (@ "Descending");
58} else {
59 NSLog (@ "同");
60}
Universal Conversion Formula
1 NSString *str14 = [NSString stringWithFormat:@"%@ lol %ld %f", str13, 100L, 3.1415];
2 NSLog(@"%@", str14);
Variable string (nsmutablestring)
// create a variable string
NSMutableString * mstr = [NSMutableString string];
// variable string splicing
[mstr appendString: @ "iPhone"];
// insert
[mstr insertString: @ "Andriod" atIndex: 2];
NSLog (@ "% @", mstr);
// delete
[mstr deleteCharactersInRange: NSMakeRange (2, 7)];
NSLog (@ "% @", mstr);
/ *
* The difference between mutable and immutable
* The operations of immutable objects all generate new objects. The mutable objects all operate on the basis of the original objects.
* /
String class in Objective-c