IOS stage study 14th day notes (NSString and NSMutableString), iosnsstring
Knowledge points of IOS Learning (oc language)
1. OC string operations
1) There are two types of strings in OC:
1. Immutable string NSString: the object content cannot be modified, but the object pointer can be changed.
2. Variable string NSMutableString: the object content can be modified.
2. NSString immutable string operations
1) assign a String constant object to the string reference NSString * str1 = @ "hello"; output format of the string object: 1 NSLog (@ "str1 = % @", str1 ).
2) initWithString can be used to construct a string in the OC to reference 1 NSString * str2 = [[NSString alloc] initWithString: str1];
3) initwithuf8string can create an OC String object from a C string and convert the C string to an OC string:
1 NSString * str3 = [[NSString alloc] initwithuf8string: "iOS"];
4) initWithFormat: you can create an OC String object int age = 20 by formatting the OC string;
1 NSString * str4 = [[NSString alloc] initWithFormat: @ "name is % @, age is % d", str1, age];
5) You can use the. length method to obtain the length of a string: 1 NSUInteger len = str1.length;
6) characterAtIndex can take a character based on the subscript, for example:
NSString * str1 = @ "hello"; unichar c = [str1 characterAtIndex: 0]; // The result is h.
7) compare is used to compare two strings. This method is case sensitive and the returned result is NSComparisonResult.
1. NSOrderedAscending indicates that the previous string is smaller than the previous one.
2. NSOrderedSame indicates that the two strings are equal.
3. NSOrderedDescending indicates that the first string is greater than the last one.
Instance code:
NSString * str1 = @ "hello"; NSString * str3 = [[NSString alloc] initwithuf8string: "iOS"]; NSComparisonResult cmp = [str1 compare: str3]; if (cmp = NSOrderedAscending) {NSLog (@ "% @ <% @", str1, str3);} else if (cmp = NSOrderedSame) {NSLog (@ "% @ = % @", str1, str3);} else if (cmp = NSOrderedDescending) {NSLog (@ "% @> % @", str1, str3);} // result: hello <iOS
8) caseInsensitiveCompare case-insensitive comparison strings; comparison strings, you can set comparison options
NSNumericSearch: If a string contains numbers, compare them by number, for example, ios5 <iso12
NSCaseInsensitiveSearch: case-insensitive comparison, for example, iOS7 = ios7
Instance code:
1 NSComparisonResult cmp = [str1 caseInsensitiveCompare: str3]; 2 str1 = @ "iOS7"; 3 str3 = @ "ios7"; 4 cmp = [str1 compare: str3 options: NSCaseInsensitiveSearch]; 5 if (cmp = NSOrderedAscending) {6 NSLog (@ "% @ <% @", str1, str3); 7} else if (cmp = NSOrderedSame) {8 NSLog (@ "% @ = % @", str1, str3); 9} else if (cmp = NSOrderedDescending) {10 NSLog (@ "% @> % @", str1, str3); 11} // result: iOS7 = ios7
9) If isw.tostring is equal to the two strings, the BOOL (YES/NO) value is returned.
Instance code:
1 NSString * str1 = @ "hello"; 2 NSString * str2 = [[NSString alloc] initWithString: str1]; 3 if ([str1 is1_tostring: str2]) {4 NSLog (@ "% @ equal % @", str1, str2); 5} // result: hello equal hello
10) uppercaseString converts lowercase letter strings into uppercase letters, for example:
1 NSString * str1 = @ "hello"; NSString * str6 = [str1 uppercaseString]; 2 // The result is: HELLO
11) lowercaseString converts uppercase letter strings to lowercase letters.
12) hasPrefix: determines whether to start with a string (as a prefix) for example:
NSString str1 = @ "www.baidu.com"; if ([str1 hasPrefix: @ "www"]) {NSLog (@ "yes");} // result: yes
13) hasSuffix determines whether to end with a string (suffix) for example:
1 NSString str1 = @ "www.baidu.com"; 2 if ([str1 hasSuffix: @ "com"]) {3 NSLog (@ "yes"); 4} // result: yes
14) substringFromIndex starts to take the sub-string (to the end) from a subscript. For example:
1 NSString str1 = @ "This is string A"; 2 NSString * res = [str1 substringFromIndex: 1]; // result: his is string
15) substringToIndex obtains a substring from the first character, excluding the characters corresponding to the current number, and obtains the first character of the current numeric subscript.
For example:
1 NSString str1 = @ "This is string A"; 2 NSString * res = [str1 substringToIndex: 2]; // result: Th3 [[str1 substringFromIndex: 8] substringToIndex: 5]; // result: strin
16) substringWithRange returns the substring according to the range
Nsange range = {8, 5}; // directly give the range value range = NSMakeRange (8, 5 ); // return the variable NSString str1 = @ "This is string A"; NSString res = [str1 substringWithRange: range] through the function; // result: strin
17) rangeOfString searches for the substring in the string and returns the position and length of the substring
Instance code:
1 NSString str1 = @ "This is string A"; 2 nsange range = [str1 rangeOfString: @ "string"]; // result: {8, 6} 3 if (range. location! = NSNotFound) {4 NSLog (@ "find"); 5} // result: find
18) Use # if 0 code segment... # Endif allows the system to skip a code segment and not execute it. For example:
# If 0 NSLog (@ "hello world !"); # EndifNSLog (@ "I am the second code !"); // Execution result: I am the second code!
19) intValue, longValue, floatValue, and doubleValue can convert string-type numbers to numerical numbers.
Example: 1 NSString * str = @ "88"; 2 int a = [str intValue];
20) concatenate multiple NSString strings
Instance code:
1 NSString * str1 = @ "My name is"; 2 NSString * str2 = @ "John Zhang"; 3 NSString * str3 = @ "I am 45 "; 4 NSString * s1 = [NSString stringWithFormat: @ "% @", str1, str2, str3]; 5 NSLog (@ "s1 = % @", s1 ); // result: s1 = My name is John Zhang I am 45
21) initWithContentsOfFile reads the file content into a string object. The first parameter is the file name (absolute path), and the second parameter is the encoding type,
The third parameter is the error callback information, for example:
1 NSString * content = [[NSString alloc] initWithContentsOfFile: "/Documents/ocfile/Person. h" encoding: NSUTF8StringEncoding error: nil];
22) print the object directly. By default, the description method is called to display the Class Name and address of the object. You can override the description method,
Note: If the direct output class object contains Chinese content, garbled characters are displayed. Therefore, class objects containing Chinese content must traverse the output.
Iii. NSMutableString variable string operations
1) initWithString: use an immutable string to create a variable String object. For example:
1 NSString * str = @ "This is string B"; 2 NSMutableString * mStr = [[NSMutableString alloc] initWithString: str];
2) insertString inserts a string at the specified subscript position, for example:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr insertString: @ "hello" atIndex: 0]; // result: hello This is string B
3) appendString append a string after the string, for example:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr appendString: @ "shanghai"]; // result: this is string B shanghai
4) appendFormat append a formatted string after the string, for example:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr appendFormat: @ "% d", 20]; // result: this is string B 20
5) deleteCharactersInRange:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr deleteCharactersInRange: NSMakeRange (0, 6)]; // result: s string B
6) replaceCharactersInRange replaces the specified range string with a new string, for example:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr replaceCharactersInRange: NSMakeRange (2, 2) withString: @ "IS"]; // result: ThIS is string B3 // replace all is in the string with IS4 nsange range = [mStr rangeOfString: @ "is"]; 5 while (range. location! = NSNotFound) {6 [mStr replaceCharactersInRange: range withString: @ "IS"]; 7 range = [mStr rangeOfString: @ "is"]; 8}
7) replaceOccurrencesOfString replaces all is within the specified range in the string with IS, for example:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr replaceOccurrencesOfString: @ "is" withString: @ "IS" options: 1 range: NSMakeRange (0, mStr. length)]; 3 // result: ThIS IS string B
8) setString:
1 NSMutableString * mStr = [[NSMutableString alloc] initWithString: @ "This is string B"]; 2 [mStr setString: @ "hello"]; // result: hello
9) NSMutableString concatenates multiple strings
Instance code:
1 NSString * str1 = @ "My name is"; 2 NSString * str2 = @ "John Zhang"; 3 NSString * str3 = @ "I am 45 "; 4 NSMutableString * s2 = [[NSMutableString alloc] initWithString: str1]; 5 6 [s2 appendString: str2]; 7 [s2 appendString: @ ""]; 8 [s2 appendString: str3]; 9 NSLog (@ "s2 = % @", s2); // result: s2 = My name isJohn Zhang I am 45