In the FoundObjective-c Foundation of Objective-c, common classes 2 -- NSString and NSMut

Source: Internet
Author: User

Continue NSString, because many are used, we should focus on understanding 1. Evaluate the string length [cpp] NSString * myName = @ "AOBAMA"; int length = (int) [myName length]; // It is found that the return value type is NSUInteger. If the conversion is not strong or warning, you do not know whether strong conversion is a common method. I hope you can tell NSLog (@ "the string length is: % d ", length); if ([myName length]> 5) {NSLog (@" AAAAA "); // print AAAAA} 2. string-related conversions change case sensitivity [cpp] NSString * string = @ "Where are You, Boy! "; // All capital NSLog (@" % @ ", [string uppercaseString]); // print where are you, BOY! // All lowercase NSLog (@ "% @", [string lowercaseString]); // print where are you, boy! // All words Are capitalized, and other letters Are lowercase NSLog (@ "% @", [string capitalizedString]); // print Where Are You, Boy! Convert a string to the basic data type [cpp] NSString * aboutFloat = @ "3.14"; NSString * aboutBool = @ "NO"; NSLog (@ "% f ", [aboutFloat floatValue]); // print 3.140000 NSLog (@ "% d", [aboutBool boolValue]); // print 0 // convert the float type to an integer NSLog (@ "% d", [aboutFloat intValue]); // print 3 merge two strings into one [cpp] // method 1 NSString * str1 = @ "Hello"; NSString * str2 = @ "World "; NSString * str3 = [[NSString alloc] initWithFormat: @ "% @", str1, str2]; NSLog (@ "str1 and str2 are merged into st R3 result: % @ ", str3); // print the result: the result of merging str1 and str2 into str3 is: hello World // method 2 NSString * str4 = [str1 stringByAppendingString: str2]; NSLog (@ "the result of merging str1 and str2 into str4 is: % @", str4 ); // print the result: str1 and str2 are merged into str4 and the result is: Hello World // connect multiple strings NSString * str5 = [str1 stringByAppendingFormat: @ "% @", str1, str2, str3]; NSLog (@ "str5 is: % @", str5); // print the result: str5 is: helloHello WorldHello World 3. Whether to start with a string hasPrefix: whether to end with a string hasSuffix [cpp] NSStri Ng * str6 = @ "who are you"; BOOL isStart1 = [str6 hasPrefix: @ "wh"]; BOOL isStart2 = [str6 hasPrefix: @ "h"]; BOOL isEnd1 = [str6 hasSuffix: @ "u"]; BOOL isEnd2 = [str6 hasSuffix: @ "o"]; NSLog (@ "Result: % d ,,, % d, % d, % d ", isStart1, isStart2, isEnd1, isEnd2); // result: 1, 0, 1 ,,, 0 4. truncate the string into an array [cpp] NSString * str7 = @ "you/have/a/baby"; NSArray * array = [str7 componentsSeparatedByString: @ "/"]; NSLog (@ "% @", array); // number The Group can print the result directly without traversing/* printing (you, have, a, baby) */5, string truncation [cpp] NSString * str8 = [str7 substringFromIndex: 2]; // blocks starting from the second location include 2, of course NSLog starting with 0 (@ "% @", str8 ); // u/have/a/baby NSString * str9 = [str7 substringToIndex: 5]; // from the start to the specified position, but does not include this location NSLog (@ "% @", str9); // you/h // intercept the nsange range = NSMakeRange (2, 3) According to the range ); // define the range first. You can also define the nsange range = {2, 3}; NSLog (@ "location .. % ld length... % ld ", range. location, range. length );/ /Location .. 2 length... 3 range. location = 3; // includes 2 range. length = 4; // NSLog (@ "location .. % ld length... % ld ", range. location, range. length); // location .. 3 length... 4 NSString * str10 = [str7 substringWithRange: range]; NSLog (@ "% @", str10); // hav 6. String Query [cpp] www.2cto. comNSRange range2 = [str7 rangeOfString: @ "have"]; nsange range3 = [str7 rangeOfString: @ "aaa"]; NSLog (@ "location1... % ld ", range 2. location); NSLog (@ "location2... % ld ", range3.location); printed result: location1... 4 location2... 9223372036854775807 the search result can be determined based on the position of the source string to be queried. Usage of NSMutableString

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.