Variable string NSMutableString and OC variable string for learning through the "snil-oc" command
NSMutableString inherits from NSString, so all NSMutableString methods of NSSting are inherited and can also be used
The following two methods are not available in NSString:
<Span style = "font-size: 14px;"> // a string with a length of 0 also indicates an empty NSMutableString * str3 = [[NSMutableString alloc] initWithCapacity: 0]; </span>
<Span style = "font-size: 14px;"> // NSMutableString * str4 = [NSMutableString], which is null in the form of class method creation; </span>
// Variable string related methods
<Span style = "font-size: 14px;"> // reset string str1 = @ "dsa"; NSMutableString * str1 = [[NSMutableString alloc] initWithString: @ "dsa"]; // str1 = @ "s1s2s3ss"; [str1 setString: @ "s1s2s3ss"]; // append the string str1 = @ "s1s2s3ss1" in formatted format "; [str1 appendFormat: @ "% d", 1]; // append a string str1 = @ "s1s2s3ss1asd"; [str1 appendString: @ "asd"]; // Delete the string range = {3, 3}; nsange range = NSMakeRange (3, 3); // str1 = @ "s1sss1asd"; the deleted string is s2s [str1 deleteCharactersInRange: range]; NSLog (@ "% @", str1); // insert a string where it is marked as 3. There is no limit on the length of the string to be inserted, the original string [str1 insertString: @ "UUUUUU" atIndex: 3]; NSLog (@ "% @", str1); // str1 = @ "s1sUUUUUUss1asd "; // Replace the string [str1 replaceCharactersInRange: NSMakeRange (3, 7) withString: @ "AAAAAAAAAAA"]; NSLog (@ "% @", str1 ); // str1 = @ "s1saaaaaaaaas1asd"; </span>
Several small programs on variable string Methods
<Span style = "font-size: 14px;">/* 10. convert the original string to lowercase letters and lowercase letters */NSMutableString * str101 = [[NSMutableString alloc] initWithFormat: @ "% @", @ "HeHeDa"]; NSMutableString * str102 = [[NSMutableString alloc] init]; for (int I = 0; I <str101.length; I ++) {unichar tempChar = [str101 characterAtIndex: I]; if (tempChar> = 65 & tempChar <= 90) {NSMutableString * tempString1 = [[NSMutableString alloc] initWithFormat: @ "% c", tempChar]; NSString * str103 = [tempString1 lowercaseString]; [str102 appendString: str103];} else {NSMutableString * tempString2 = [[NSMutableString alloc] initWithFormat: @ "% c", tempChar] NSString * str104 = [tempString2 uppercaseString]; [str102 appendString: str104] ;}} NSLog (@ "% @", str102);/* 11. sort strings alphabetically, for example, input @ "aczabczab" output @ "aaabbcczz" */NSMutableString * str111 = [[NSMutableString alloc] initWithFormat: @ "% @", @ "aczabczab"]; for (int I = 0; I <str111.length-1; I ++) {for (int j = I + 1; j <str111.length; j ++) {unichar ci = [str111 characterAtIndex: I]; unichar cj = [str111 characterAtIndex: j]; if (ci> cj) {nsange iRange = {I, 1}; nsange jRange = {j, 1}; [str111 replaceCharactersInRange: iRange withString: [[NSString alloc] initWithFormat: @ "% c", cj]; [str111 replaceCharactersInRange: jRange withString: [[NSString alloc] initWithFormat: @ "% c", ci] ;}} NSLog (@ "% @", str111);/* 12. use NSLog to print the 99 multiplication table */for (int I = 1; I <10; I ++) {NSMutableString * str121 = [[NSMutableString alloc] init]; for (int j = 1; j <= I; j ++) {[str121 appendFormat: @ "% d * % d = % d \ t", j, I, I * j] ;}nslog (@ "% @", str121) ;}</span>
This is the string method. As the teacher said before, everything is a string. Therefore, strings will certainly be used frequently in the future, and there are not many methods in themselves. It should be okay to work diligently.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.