iOS Development-objc-nsstring

Source: Internet
Author: User

NSString is a common class in the OC Language Foundation framework, and I divide the common methods of nsstring into the functions of creating strings, initializing strings, judging and comparing strings, uppercase and lowercase conversions, string interception, and type conversion, based on the functionality of each method. Its methods according to function (the role of each method I have in the back of the detailed remarks, in fact, according to the Hump command also basically can see the role of each method), some methods with parameters, some without parameters, the use of methods are easy, in Xcode programming will automatically match: 1?? To create a string:

1 string: Create an empty string

2stringwithstring: Using strings to create strings3stringWithFormat: Creating a string in NSLog format2?? Initialization string: 1init: Create an empty string and initialize it2initwithstring: Initializing Strings with strings3Initwithformat: Formatting strings in nslog format3?? String judgments and Comparisons: 1isequaltostring: Comparing Strings for equality2Hasprefix: Determines whether a string starts with a character3Hassuffix: Determines whether a string ends with a character4Compare: Comparing the size of a string5Caseinsensitivecompare: Comparisons that do not consider the case4?? String Case Conversion 1uppercasestring: Lowercase letters converted to uppercase letters2lowercasestring: Uppercase letters converted to lowercase3capitalizedstring: Capitalize the first letter of each word5?? Interception: 1Substringtoindex: From the beginning of the string to the specified position2Substringfromindex: Truncated to the end of the string starting at the specified position3substringwithrange: Returns a substring based on a specified range4Characteratindex: Returns the character in the string to which the index number points6?? Conversion Type: 1doublevalue/floatvalue: Returns a value converted to a floating-point type2intvalue: Returns the value converted to shaping3boolvalue: Returns a value converted to a Boolean type7?? Other: 1Length: To find string lengths2stringbyappendintstring: Add a new string after the string3rangeofstring: Find if the string contains other characters4Stringbytrimmingcharacterinset: Remove spaces or enterThe above seven methods are specifically implemented as follows:
1/NSString 2//NSString *string1 = [NSString stringwithstring:@ "Appbear"];  3//NSLog (@ "%@", string1); 4 5 6 NSString *string2 = @ "Bearapp"; 7 NSLog (@ "%@"), string2); 8 9 NSString *string3 = [NSString stringwithformat:@ "Today is%d", 5]; NSLog (@ "%@"), string3); 12 13//Connection two strings nsstring *string4 = [NSString stringwithformat:@ "Today is%@", string2]; NSLog (@ "%@", String4); 16 17 18//Determine if the string is the same as nsstring *codestring = @ "cocochina123"; NSString *codestring2 = @ "cocochina123"; If([codestring isequaltostring:codestring2]) {NSLog (@ "Two strings equal"); }else{NSLog (@ "Two strings not Equal"); 26} 27 28//Compare addresses are identical if (codestring2==codestring) {NSLog (@ "= =")); }else{NSLog (@ "! ="); 33} 34 35//Case Conversion 36//Convert all to uppercase PNS nsstring *up =[Codestring uppercasestring]; NSLog (@ "%@", up); 39//Convert all to lowercase nsstring *low =[CodeString2 lowercasestring]; NSLog (@ "%@", low); 42//All initial capitals nsstring *cap =[String4 capitalizedstring]; NSLog (@ "%@", CAP); NSString *stri1 = @ "ABCDEFG"; NSString *stri2 = @ "BBCDEFG"; if (stri1==STRI2) {NSLog (@ "Two strings Same"); }else{NSLog (@ "two strings different")); 54} if([Stri1 Isequaltostring:stri2]) {NSLog (@ "Two strings equal")); }else{NSLog (@ "Two strings Not Equal")); 59}//Nscomparisonresult is an enumerator for nscomparisonresult Resut =[Stri1 Caseinsensitivecompare:stri2]; NSLog (@ "%ld"), Resut); 64 */65//intercept string NSString *string1 = @ "It's sunny today."; NSString *string2 = @ "It's overcast today."; NSLog (@ "%@", [string1 substringfromindex:2]); NSLog (@ "%@", [string2 substringtoindex:2]); Nsrange range = {1,3}; NSLog (@ "%@", [string1 substringwithrange:range]); 73 74//Stitching two strings nsstring * lastoutstring = [string1 stringbyappendingstring:string2]; NSLog (@ "%@" , lastoutstring); 77 78//Remove the space at both ends of the string NSString *l asttrimstring =  [string1 stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]; NSLog (@ "%@" , lasttrimstring); 81 82 83//Replace the contents of the string by nsstring *replace = [string1 stringbyreplacingoccurrencesofs tring:@ "Today" withstring:@ "Yesterday" ]; NSLog (@ "%@" , replace); 86 87 88//Search string The first method of the Nsrange range1 = [string1 rangeofstring:@ "Sunny" ]; e1.location!=  nsnotfound) {NSLog (@ "found Sunny" ),}else  {NSLog (@ "No Sunny" ); 94 } 95 96// Search string The second method is nsrange range2 = [string1 rangeofstring:@ "Sunny" ]; 98 if (range2.length!=0 ) {NSLog (@ "Found Sunny%lu,% Lu ", (unsigned long) range2.location, (unsigned long ) range2.length);}else  {101 NSLog (@" Not found fine "); 102}                 

iOS Development-objc-nsstring

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.