Dark Horse programmer--oc Common Class Nsstring/nsmutablestring "foundation Framework Class"

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

nsstring/nsmutablestring : OC string Processing Core class

nsstring/nsmutablestring is a class in the foundation framework and is OBJECTIVE-C instringprocessingof theCore Class. The biggest differences between the two classes are:NSStringafter the assignment is created, the string, in addition to being re-assigned, cannot be dynamically changed in its content and length. nsmutablestringAfter you create an assignment, you can dynamically change its content and length. Where theNsmutablestring class inherits the NSString class (in the NSString header file (NSString.h file), you can see:@interface Nsmutablestring: nsstring This code ). So the Nsmutablestring class can use all the methods in NSString.

Instance code:

NSString *str1 [email protected] "The first to create string";//Creating a nsstring string nsmutablestring *mstr = [[nsmutablestring] alloc]initwithstring:@ "The first to create"];//creates nsmutablestring string <span style= "Font-size:18px;color: #ff6666 ;" >//str1 The first letter of the string and re-assigns it to STR1 (cannot be modified directly on the original str) </SPAN>STR1 =[STR1 capitalizedstring];//re-assignment can change the contents of NSString. No assignment cannot change its content and length [mstr appendstring:@ "mutablestring"];<span style= "Font-size:18px;color: #ff6666;" >//appended to the original MSTR, changing the content and length of the MSTR </span>

1.NSString Creation

How to create:

<span style= "Font-size:18px;color: #ff6666; Background-color:rgb (255, 255, 255);" >, Direct Assignment </span>nsstring *str1 [email protected] "The first to create string"; <span style= "font-size : 18px;color: #ff6666; " > second, commonly used object creation method </span>nsstring *str2 = [[NSString alloc]initwithstring:@ "The second way to create string"];< Span style= "Font-size:18px;color: #ff6666;" > Three, splicing method to create </span>nsstring *STR3 = [[NSString alloc]initwithformat:@ "%d creation:%d years%d months%d days",3,2015,4,11];< Span style= "Font-size:18px;color: #ff6666;" > to convert C-language strings to OC characters </span>nsstring *STR4 = [[NSString alloc]initwithutf8string: ' This is a const char * (c-language string) is converted to NSString to create the string "];<span style=" Font-size:18px;color: #ff6666; " > read the creation of local files, read the contents of a.txt files under the/users/mac/desktop/ios_study path, use UTF8 encoding </span>nsstring *STR5 = [NSString alloc]initwithcontentsoffile:@ "/users/mac/desktop/ios_study/a.txt" encoding:nsutf8stringencoding error:nil];< Span style= "Font-size:18px;color: #ff6666;" > Vi. How to create a read resource </span><span style= "Color:rgb (255, 102, 102); font-size:18px; Font-family:arial, Helvetica, Sans-serif; Read the contents of the B.txt file under the/users/mac/desktop/ios_study path, using UTF8 encoding </span><span style= "font-size:18px;color:# ff6666; " ></span>nsurl *url = [[Nsurl alloc]initwithstring:@ "File:///Users/Mac/Desktop/IOS_study/b.txt"]; NSString *STR6 = [[NSString alloc]initwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil];

Example Demonstration:

Created by Mac on 15/4/11.//Copyright (c) 2015 Hua_san.  All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool        {///one, direct assignment nsstring *str1 [email protected] "The first to create string";        Second, commonly used object creation method NSString *str2 = [[NSString alloc]initwithstring:@ "The second way to create string"];        Three, splicing method to create nsstring *STR3 = [[NSString alloc]initwithformat:@ "%d creation Method:%d years%d months%d days", 3,2015,4,11]; Four, the C language string to the creation of OC characters nsstring *STR4 = [[NSString alloc]initwithutf8string: "This is a const char * (c-language string) converted to nsstring the way to create        Build string "]; Five, read the creation of local files, read the contents of the A.txt file under the/users/mac/desktop/ios_study path, using UTF8 encoding nsstring *STR5 = [NSString Alloc]initwithcont        entsoffile:@ "/users/mac/desktop/ios_study/a.txt" encoding:nsutf8stringencoding Error:nil]; The A.txt content under Path/users/mac/desktop/ios_study is: This is the 5th way to create, by reading a file NSString//VI, how to create a read resource/** 1.U  RL: Resource Path       2. Presentation method: protocol Header://+ path 3. Common protocol Header: file (get local Resource), FTP (Get resources on FTP server), HTTP (Get resources on HTTP Server) 4. Get Baidu Image: http://image.b Aidu.com..../xxx.png 5. This example reads a local file using UTF8 encoding. URL resource path using method 2 stitching get: File:///Users/Mac/Desktop/IOS_study/b.txt 6. The contents of B.txt in the path/users/mac/desktop/ios_study: This is the sixth way to create , the local file is read through the URL resource, the protocol header is: file://*/Nsurl *url = [[Nsurl alloc]initwithstring:@] File:///Users/Mac/Desktop/IOS_st        Udy/b.txt "];                NSString *STR6 = [[NSString alloc]initwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil];        NSLog (@ "%@", str1);        NSLog (@ "%@", str2);        NSLog (@ "%@", STR3);        NSLog (@ "%@", STR4);        NSLog (@ "%@", STR5);    NSLog (@ "%@", STR6); } return 0;}

Output Result:

2015-04-11 18:53:17.239 02-nsstring class [1360:64404] The first to create string2015-04-11 18:53:17.240 02-nsstring class [1360 : 64404] The second way to create string2015-04-11 18:53:17.241 02-nsstring class [1360:64,404] 3rd creation: April 11, 2015 2015-04-11 18:53:17.241 02-nsstring class [1360:64,404] This is a const char * (c-language string) converted to NSString to create a string 2015-04-11 18:53:17.241 02-nsstring class [ 1360:64,404] This is the 5th way to create a nsstring2015-04-11 18:53:17.241 02-nsstring class by reading a file [1360:64,404] This is the sixth way to create a local file through a URL resource, The protocol header is: file://


2.NSString Intercept

Example Demonstration:

Created by Mac on 15/4/11.//Copyright (c) 2015 Hua_san.  All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool        {NSString *str1 [email protected] "The first to create string"; <span style= "Font-size:18px;color: #ff6666;"        >//gets the length of the string </span> nsinteger lenth = [str1 length]; <span style= "Font-size:18px;color: #ff6666;" >//based on index value, get character </span> unichar ch = [STR1 characteratindex:2];//index value starting from 0 <span style= "font-size:1 8px;color: #ff6666; " >//intercept string tail </span> nsstring *end = [str1 substringfromindex:17];//index value starts from 0 <span style= "font-siz E:18px;color: #ff6666; "       >//Intercept header </span> nsstring *first = [str1 substringtoindex:3]; <span style= "FONT-SIZE:18PX;" > <span style= "color: #ff6666;"        >//gets the range of the string "to" in the STR1 string </span></span> nsrange range = [str1 rangeofstring:@ "to"]; <span StylE= "Font-size:18px;color: #ff6666;"        >//the string </span> nsstring *to_str = [str1 Substringwithrange:range] by range, from str1        NSLog (@ "%@", str1);        NSLog (@ "string%@ of length:%ld", str1,lenth);        NSLog (@ "3rd character:%c", ch);        NSLog (@ "starts from the 18th character of the string%@, intercepts its tail:%@", str1,end);        NSLog (@ "string%@, intercept the first 3 words Fu De:%@", Str1,first);        NSLog (@ "\" to\ "string in the initial position in%@:%ld, occupies a length of:%ld", str1,range.location,range.length);   NSLog (@ "for string%@, intercept range (initial position:%ld, Length:%ld) to get string:%@", STR1,RANGE.LOCATION,RANGE.LENGTH,TO_STR); } return 0;}

Output Result:

2015-04-11 19:31:20.065 02-nsstring class [1641:77263] The first to create string2015-04-11 19:31:20.066 02-nsstring class [1641 : 77263] String The first to create string is: 302015-04-11 19:31:20.066 02-nsstring class [1641:77,263] The 3rd character is: e2015-04-11 The 19:31:20.067 02-nsstring class [1641:77,263] starts with the 18th character of the string, the first-to-create string, and intercepts its tail: Create string2015-04-11 19:31:20.067 02-nsstring class [1641:77,263] to the string the first "to create" string, intercept the top 3 characters Fu De: the2015-04-11 19:31:20.067 02- The initial position of the NSString class [1641:77263] "to" string in the first-to-create string: 14, occupying length: 22015-04-11 19:31:20.067 02-nsstring class [ 1641:77,263] to string the first-to-create string, intercept the range (initial position: 14, Length: 2) Gets the string: to


Comparison of 3.NSString Judgments

Example Demonstration:

<span style= "FONT-SIZE:12PX;" >//Created by Mac on 15/4/11.//Copyright (c) 2015 Hua_san.  All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool        {NSString *str = @ "The first to create string";    NSString *STR1 = @ "the"; </span><span style= "Font-size:18px;color: #ff6666;" >/*1. Determine if the string str is empty </span><span style= "font-size:12px;" > Str==nil str.length ==0 */</span><span style= "Font-size:18px;color: #ff6666;" &GT;//2. Determine if the string str starts with str1 </span><span style= "FONT-SIZE:12PX;"    > BOOL preFix = [str hasprefix:str1]; </span><span style= "Font-size:18px;color: #ff6666;" &GT;//3. Determine whether the string str ends with str1 </span><span style= "FONT-SIZE:12PX;"    > BOOL sufFix = [str hassuffix:str1]; </span><span style= "Font-size:18px;color: #ff6666;" &GT;//4. Determine whether two strings are equal </span><span style= "FONT-SIZE:12PX;" > BOOL IsequaL = [str isequaltostring:str1];   NSLog (@ "\nprefix:%d\nsuffix:%d\nisequal:%d", prefix,suffix,isequal); } return 0;} </span>

Output Result:

<span style= "FONT-SIZE:12PX;" >2015-04-11 22:03:00.326 02-nsstring class [2069:115725] Prefix:1suffix:0isequal:0</span>

4.NSString Write file

Example Demonstration:

<span style= "FONT-SIZE:12PX;" >//Created by Mac on 15/4/11.//Copyright (c) 2015 Hua_san.  All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool        {NSString *str = @ "The first to create string";        NSString *str1 = @ "\ n This is a concatenation of strings"; </span><span style= "Font-size:18px;color: #ff6666;" >//writes the STR string to the local file. </span><span style= "FONT-SIZE:12PX;" > BOOL iswiter = [str writetofile:@ "/users/mac/desktop/ios_study/c.txt" Atomically:yes Encoding:nsutf8stringenco        Ding Error:nil]; if (iswiter) NSLog (@ "The first time the file was written successfully!        ");        else NSLog (@ "Failed to write the file for the first time"); Here the action file path is/users/mac/desktop/ios_study/c.txt, if the file does not exist, re-create, if there is a direct write, write to the file will first empty the contents of the file. <span style= "color: #ff6666;" >atomically:yes is quite open atomic operation </span> (if write interrupts, all files are not written).                Encoding: Use UTF-8 to encode. </span><span style= "Font-size:18px;color: #ff6666;" >//Read file </span><span style= "font-size:12px; " > NSString *readstring = [[NSString alloc]initwithcontentsoffile:@ "/users/mac/desktop/ios_study/c.txt" encoding:        Nsutf8stringencoding Error:nil]; </span><span style= "Font-size:18px;color: #ff6666;" >//str1 Stitching at the end of ReadString (the read-out string) </span><span style= "FONT-SIZE:12PX;"        > str1 = [readString stringbyappendingstring:str1]; </span><span style= "Font-size:18px;color: #ff6666;" >//Write file </span><span style= "FONT-SIZE:12PX;"  > iswiter = [str1 writetofile:@ "/users/mac/desktop/ios_study/c.txt" Atomically:yes encoding:nsutf8stringencoding        Error:nil]; if (iswiter) NSLog (@ "The second time the file was written successfully!        ");           else NSLog (@ "Second write to file failed"); } return 0;} </span>

Output Result:

<span style= "FONT-SIZE:12PX;" >2015-04-11 22:42:57.792 02-nsstring class [2,341:128,870] wrote the file successfully for the first time! 2015-04-11 22:42:57.794 02-nsstring class [2,341:128,870] The second time the file was written successfully! </span>
the contents of the local file C.txt (the file just written) are as follows:

The first-to-create string
It's a string of stitching up.


5.Common operations for nsmutablestring variable strings

Example Demonstration:

<span style= "FONT-SIZE:12PX;" >//Created by Mac on 15/4/11.//Copyright (c) 2015 Hua_san.  All rights reserved.//#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {</span><span style= "font-size:18px;" > <span style= "color: #ff6666;" >//Create variable string </span></span><span style= "FONT-SIZE:12PX;"        > nsmutablestring *str=[[nsmutablestring alloc]initwithstring:@ "My name is Object-c"];        NSLog (@ "Newly created string str:%@", str); </span><span style= "Font-size:18px;color: #ff6666;" >//adds a string at its end, its length changes, and its contents change </span><span style= "FONT-SIZE:12PX;"        > [str appendstring:@ "JAVA C + +"];        NSLog (@ "Add content after string str:%@", str); </span><span style= "Font-size:18px;color: #ff6666;" >//finds the range of object-c in the STR string </span><span style= "FONT-SIZE:12PX;"        > Nsrange range =[str rangeofstring:@ "object-c"]; </span><span style= "Font-size:18px;color: #ff6666;" >//Delete Object-c</span><span style= "font-size:12px;"        > [str deletecharactersinrange:range];           NSLog (@ "Delete object-c after the string str:%@", str); } return 0;} </span>

Output Result:

<span style= "FONT-SIZE:12PX;" >2015-04-11 23:03:56.679 02-nsstring class [2,490:136,327] newly created string str:my name is object-c2015-04-11 23:03:56.680 02- NSString class [2,490:136,327] Add content after string str:my name is object-c JAVA c++2015-04-11 23:03:56.680 02-nsstring class [2,490:136,327] Remove Object-c string str:my name is  JAVA c++</span>

Dark Horse programmer--oc Common Class Nsstring/nsmutablestring "foundation Framework Class"

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.