A detailed explanation of NSString and nsmutablestring in the Foundation framework

Source: Internet
Author: User

NSString: immutable strings

The simplest way to create a string:

NSString *str = @ "Itcast"; The string created by OC string OC is an object of the NSString class.

Char *name = "Itcast"; This is the C language string

NSLog (@ "I'm in%@ class", str);//%@ is used to output OC objects

NSLog (@ "%s", name); %s is used to output C-language strings

Benefits of using OC strings:

Manipulating strings in the form of objects;

1     // Define a string 2     @" Jack " ; 3     4     // computes the number of characters in a string and outputs the result 5     NSLog (@ "%ld", str.length);

The creation of a string:

First: Quickly create

NSString *str = @ "Jack";

The second type: Create in a formatted way

NSString *str = [[NSString alloc] Initwithformat:@ "My age is%d", +]; This is the use of object methods

NSString *str = [NSString stringwithformat:@ "My age is%d", +]; This is the use of class methods

The mutual reload between OC string and C language string

  

1     //This is the conversion of a C-language string to a bit OC string2     Char*ch ="I love oc!";3     4NSString *str1 = [NSString stringwithutf8string:"I love oc!"];5NSString *STR2 =[NSString stringwithutf8string:ch];6NSLog (@"str1 =%@, str2 =%@", str1, str2);7     8     //This is to replace the OC string with the C language string9     Const Char*newch1 =[str1 utf8string];Ten     Const Char*NEWCH2 =[str2 utf8string]; OneNSLog (@"newch1 =%s, Newch2 =%s", Newch1, NEWCH2);

The result of the operation is:
2015-01-05 20:53:46.820 Foundation frame [444:303] str1 = I love oc!, str2 = I love oc!
2015-01-05 20:53:46.822 Foundation frame [444:303] newch1 = I love oc!, newch2 = I love oc!
Program ended with exit code:0

To read a string from a file:

NSString *str = [NSString stringwithcontentsoffile:@ "/users/mac/desktop/sb.txt" encoding:  Nsutf8stringencoding error:nil]; This is a class method

Stringwithcontentsoffile: An absolute path is passed in later;

encoding: The following parameter is a coding method. when we want to read the string in the Chinese language when we want to use the encoding method is:nsutf8stringencoding

Error:nil indicates that if the file is not found, the returned result is null and void.

To write a string to a file: 

NSString *str = @ "I love oc!";

[@ "I love oc!" Writetofile:@ "/users/mac/desktop/sb.txt" Atomically:yes Encoding: Nsutf8stringencoding Error:nil];

[str writetofile:@ "/users/mac/desktop/sb.txt" atomically:YES encoding : nsutf8stringencoding error:nil];

The effect of the two sentences is the same.

WriteToFile: An absolute path is passed in later

atomically:YES indicates that files are only written when file creation is successful

Encoding: nsutf8stringencoding When we have Chinese, we use this coding method.

Error:nil error when returning null

Knowledge Supplement:

Protocol Header: Resource path

URL: Resource Path

file://This is the file path. When it was written here, there were three///

ftp://This is the server path.

The universal way to read a string:

    

A detailed explanation of NSString and nsmutablestring in the Foundation framework

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.