Foundation framework nsstring common Summary

Source: Internet
Author: User

During iOS development, it will deal with nsstring from the beginning. Here, we will summarize the common nsstring methods and add the methods used in later learning.

The nsstring class is an immutable string in the foundation framework. After nsstring is created, the content of the string cannot be changed, which corresponds to the nsmutablestring class.

1. Create an nsstring object

1. Use stringwithformat to create strings. This method can dynamically build strings and PASS Parameters to strings (recommended)

1 nsstring * STR = [nsstring stringwithformat: @ "I have inquired about your recent life. % @", @ "love you"]; 2 nslog (@ "% @", str );

2. Other creation methods

1 // convert the C string to the OC String object 2 char * P = "I love Beijing"; 3 nsstring * teststr = [[nsstring alloc] initwithformat: @ "% s ", p]; 4 nslog (@ "teststring = % @", teststr); 5 6 // convert the OC String object into a C string 7 const char * newp = [teststr utf8string]; 8 nslog (@ "newp = % s", newp ); 9 10 // class method corresponding to the instance method of the object constructed using the format control string 11 // The object created using the class method in the foundation framework, managed memory 12 nsstring * str2 = [nsstring stringwithformat: @ "This Is a oc sting % d", 109]; 13 nslog (@ "str2 = % @", str2 ); 14 // =================================================== =========15 // use an existing String object to construct a new String object 16 nsstring * STR = @ "I'm a OC string "; 17 nsstring * str3 = [[nsstring alloc] initwithstring: Str]; 18 nslog (@ "str3 = % @", str3 ); 19 // corresponding class method 20 nsstring * str4 = [nsstring stringwithstring: Str]; 21 nslog (@ "str4 = % @", str4 ); 22 23 // ============================================ =========24 // use a C string to construct an OC String object 25 nsstring * str5 = [[nsstring alloc] initwithuf8string: "This is a C string"]; 26 nslog (@ "str5 =%@", str5 ); 27 // corresponding class Method 28 nsstring * str6 = [nsstring stringwithuf8string: "This is a C string"]; 29 nslog (@ "str6 = % @", str6 ); 30 31 // ============================================ ===========32 // construct a String object 33 34 nsstring * filepath = @ "/users/qianfeng/desktop/test.txt" using the file content "; 35/* 36 first parameter: Path of the file to be read 37 second parameter: Specify the encoding set as nsutf8string38 third parameter: used to store the error message 39 */40 nserror * error = nil; 41 nsstring * str7 = [nsstring stringwithcontentsoffile: filepath encoding: nsutf8stringencoding error: Nil]; 42 nslog (@ "str7 =%@", str7 );

Ii. Common nsstring operations

1 // obtain the string length (length) 2 nsstring * STR = [nsstring stringwithformat: @ "I have inquired about your recent life. % @", @ "love you"]; 3 nsuinteger lenth = [STR length]; 4 nslog (@ "% lD", lenth); 5 nslog (@ "% @", STR ); 6 // nsange 7/* 8 in the foundation framework, the following 9 typedef struct _ nsange {10 nsuinteger location; // represents position 11 nsuinteger length; // length 12} nsange; 13 */14 nsstring * str2 = @ "Do you have any exercise when you are busy? "; 15 nsstring * substring = @" busy "; 16 // search for substrings (starting from 0) 17 nsange range = [str2 rangeofstring: substring]; 18 // print the struct method in the framework 19 nslog (@ "% @", nsstringfromrange (range); 20 // use the Framework to create range21 nsange newrange = nsmakerange (5, 3); 22 nsstring * newtemp = [str2 substringwithrange: newrange]; 23 nslog (@ "% @", newtemp ); 24 // determine whether to find the substring 25 if (range. location = nsnotfound) {26 nslog (@ "can't Fount"); 27} else {28 nslog (@ "got It! "); 29} 30 // string comparison (size) 31 nsstring * stra = @" Chengdu "; 32 nsstring * strb = @" Chongqing "; 33 // compare: the Return Value Type of the method is nscomparisonresult (enumeration value), where 34 // nsorderedsame indicates equal nsorderedascending indicates Ascending Order (less than) 35 // nsordereddescending indicates descending order (greater) 36 IF ([stra compare: strb] = nsorderedsame) {37 nslog (@ "equal"); 38} else if ([stra compare: strb] = nsorderedascending) {39 nslog (@ "less than"); 40} else {41 nslog (@ "greater than"); 42} 43 // string comparison (equal or not) 44 bool isequle = [stra isw.tostring: strb]; 45 nslog (@ "the result is: % d", isequle ); 46 47 // determine the prefix Suffix of the string 48 nsstring * filestring = @ "/users/student/desktop/file.txt "; 49 // determine the prefix (hasprefix :) Method 50 bool isprefix = [filestring hasprefix: @ "/users"]; 51 // determine the suffix (hassuffix :) method 52 bool issuffix = [filestring hassuffix :@". TXT "]; 53 nslog (@" isprefix = % d, issuffix = % d ", isprefix, issuffix ); 54 // The String Conversion method provided by the 55/* 56 framework is as follows: 57-(double) doublevalue; 58-(float) floatvalue; 59-(INT) intvalue; 60-(nsinteger) integervalue ns_available (10_5, 2_0); 61-(long) longvalue ns_available (10_5, 2_0); 62-(bool) boolvalue ns_available (10_5, 2_0); 63 */64 nsstring * strtonum = @ "12345.234"; 65 int result = [strtonum intvalue]; 66 nslog (@ "result = % d", result );

 

Foundation framework nsstring common Summary

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.