iOS development OC (17)--foundation (2) NSString and simple string read and write

Source: Internet
Author: User

NSString: Immutable string

Nsmutablestring: variable string

Note:The URL cannot read the file named Chinese, otherwise null, cannot be read, to change to English

// URL read file is omnipotent

Here are some of the features of the Code

1. Create and read

voidstringcreate () {/*1. How to create a string*/NSString*S1 =@"Jack"; //nsstring *s2 = [[NSString alloc] initwithstring:@ "Jack"];NSString*S3 = [[NSString alloc] Initwithformat:@"Age is %d",Ten]; //C String ---OC stringNSString *S4 = [[NSString alloc] initwithutf8string:"Jack"]; //OC String ---C string    Const Char*cs =[S4 Utf8string]; //Read File//nsutf8stringencoding used in Chinese can use this codeNSString *S5 = [[NSString alloc] Initwithcontentsoffile:@"/users/apple/desktop/1.txt"encoding:nsutf8stringencoding Error:nil]; //URL: Resource Path//protocol Header://Path//File://    //ftp://    // Http://weibo.com/a.png    // http://www.baidu.com        //This is the read local file//Nsurl *url = [[Nsurl alloc] initwithstring:@ "file:///users/apple/desktop/1.txt "];Nsurl*url = [Nsurl Fileurlwithpath:@"/users/apple/desktop/1.txt"]; NSString*S6 =[[NSString alloc] Initwithcontentsofurl:url encoding:nsutf8stringencoding Error:nil]; NSLog (@"s6=\n%@", S6); /*provides a class method to quickly return an object to someone else, without the need for alloc ... Initwith .... Very slow generally there is a class method paired with an object method (class method usually begins----urlwithstring.     ) [Nsurl urlwithstring:<# (NSString *) #>];     [NSString stringwithformat:@ ""]; [NSString stringwithcontentsoffile:<# (NSString *) #> encoding:<# (nsstringencoding) #> error:<# (          Nserror *__autoreleasing *) #>]; */}    

2. Write

voidStringexport () {//Write String (try not to create. rtf, will not open)[@"Jack\njack"WriteToFile:@"/users/apple/desktop/my.txt"atomically:yes encoding:nsutf8stringencoding Error:nil]; NSString*str =@"4234234"; Nsurl*url = [Nsurl Fileurlwithpath:@"/users/apple/desktop/my2.txt"]; [Str writetourl:url atomically:yes encoding:nsutf8stringencoding Error:nil];//atomically Atomic Nature//every line in the file is \ n}

3. Simple Application

intMain () {nsmutablestring*S1 = [Nsmutablestring stringWithFormat:@"My age is ten"]; //stitching the contents to the back of the S1[S1 appendString:@" One"]; //get the scope of isNsrange range = [S1 rangeofstring:@" is"];        [S1 Deletecharactersinrange:range]; NSString*S2 = [NSString stringWithFormat:@"Age is ten"]; NSString*S3 = [S2 stringbyappendingstring:@" One"]; NSLog (@"s1=%@, s2=%@", S1, S2); return 0;}

There are a lot of uses, please refer to the official documents, that is the true path of iOS, the origin.

iOS development OC (17)--foundation (2) NSString and simple string read and write

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.