Objc-C Knowledge Point Review 4 NSString and NSMutableString

Source: Internet
Author: User

Objc-C Knowledge Point Review 4 NSString and NSMutableString

1. NSString // NSString: unchangeable string. Once created, you cannot modify // initialization method // create an empty string NSString * string1 = [[NSString alloc] init]; NSLog (@ "% @", string1); // create an NSString object NSString * string2 = [[NSString alloc] initWithString: @ "iPhone"] based on the given constant string; NSLog (@ "% @", string2); // constant area string NSString * string3 = @ "iPhone"; NSLog (@ "% @", string3 ); NSLog (@ "% p", string3); // output address // if other variables exist in the string to be created, you need to create NSInteger version = 6 using initWithFormat; NSS Tring * string4 = [[NSString alloc] initWithFormat: @ "iPhone % ld", version]; NSLog (@ "% @", string4 ); // convenience constrtor NSString * str1 = [NSString string]; NSLog (@ "% @", str1); NSString * str2 = [NSString stringWithString: @ "iOS"]; NSLog (@ "% @", str2); NSString * str3 = @ "iOS"; NSLog (@ "% @", str3); NSString * str4 = [NSString stringWithFormat: @ "iOS % ld", version]; NSLog (@ "% @", str4); // obtain the string length // NSUInteger length = [Str4 length]; // call the getter method // NSLog (@ "% lu", length); NSLog (@ "% lu", str4.length ); // point syntax // determine whether the string starts or ends with a pin string * str5 = @ "abcdpp123"; // constant area string NSString * str6 = [[NSString alloc] initWithFormat: @ "abcdpp123"]; // heap string // prefix BOOL result = [str6 hasPrefix: @ "abd"]; NSLog (@ "% d", result ); // suffix result = [str6 hasSuffix: @ "23"]; NSLog (@ "% d", result); // string truncation (substring truncation) // obtain the abc (prefix string) NSString * subStr1 = [str6 s UbstringToIndex: 3]; NSLog (@ "% @", subStr1); // obtain 123 (end string) NSString * subStr2 = [str6 substringFromIndex: 6]; NSLog (@ "% @", subStr2); // obtain dpp (intermediate string) // nsange range = {3, 3}; NSString * subStr3 = [str6 substringWithRange: NSMakeRange (3, 3)]; NSLog (@ "% @", subStr3); // concatenated string NSString * newSubStr = [subStr1 stringByAppendingString: subStr2]; NSLog (@ "% @", subStr1); NSLog (@ "% @", subStr2); NSLog (@ "% @", newSubStr ); NSLog (@ "% p", newSubStr); // output address NSString * newSubStr1 = [NSString stringWithFormat: @ "% @", subStr1, subStr2]; NSLog (@ "% @", newSubStr1); NSLog (@ "% p", newSubStr1 ); // output address // replace string // @ "NBA"-> @ "CBA"; NSString * str7 = @ "NBANNN"; NSString * newSubStr2 = [str7 stringByReplacingOccurrencesOfString: @ "N" withString: @ "C"]; NSLog (@ "% @", newSubStr2) // string comparison // @ "NBA" and @ "CBA "; // NSString * str8 = @ "NBA"; // NSSt Ring * str9 = @ "CBA"; NSString * str8 = [[NSString alloc] initWithFormat: @ "NBA123456aa"]; NSString * str9 = [[NSString alloc] initWithFormat: @ "NBA123456aa"]; // isw.tostring compares the string content if ([str8 isw.tostring: str9]) {NSLog (@ "string equal ");} else {NSLog (@ "inconsistent");} // = compare whether the two objects are consistent. if the objects are consistent, their content must be consistent if (str8 = str9) {NSLog (@ "Equal string");} else {NSLog (@ "inconsistent ");} // convert the string and value types to each other // @ "123" // convert the string and value type to a number. If the value is not a number, the conversion is stopped. For NSString * str10 = @ "123a456"; int a = [str10 intValue]; NSLog (@ "% d", a); NSInteger B = [str10 integerValue]; NSLog (@ "% ld", B); // case-sensitive conversion operation // @ "ios"-> @ "iOS" NSString * str11 = @ "ios "; // all upper case NSString * newStr = [str11 uppercaseString]; NSLog (@ "% @", newStr); // upper case NSLog (@ "% @", [str11 capitalizedString]); // all lowercase nslogs (@ "% @", [str11 lowercaseString]); 2. NSMutableString: Variable string // @ "iPhone" // create variable string, container The size is an expected value. Generally, it is written as 0, so that the system allocates space NSMutableString * mString = [[NSMutableString alloc] initWithCapacity: 0]; NSLog (@ "% @", mString); // Add the string [mString appendString: @ "iPhone"]; NSLog (@ "% @", mString); [mString appendFormat: @ "iPhone % ld ", version]; NSLog (@ "% @", mString); // Insert the string [mString insertString: @ "" atIndex: 6]; NSLog (@ "% @", mString); // Delete the string [mString deleteCharactersInRange: NSMakeRange (0, 7)]; NSLo G (@ "% @", mString); // specify ". NSMutableString * name = [[NSMutableString alloc] initWithCapacity: 0]; [name appendString: @ "blue ocean .png"]; if ([name hasPrefix: @ "png"]) {NSLog (@ "ending with 'png '"); [name replaceCharactersInRange: NSMakeRange (name. length-3, 3) withString: @ "jpg"];} else {NSLog (@ "not ending with 'png"); [name appendString :@". jpg "];} NSLog (@" % @ ", name );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.