The NSString of Oc--foundation

Source: Internet
Author: User

Tip: Ns:nextstep cg:coregraphics

NSString: Immutable string nsmutablestring: mutable string

URL local file header: file://URL Network File header:/http

int main (int argc, const char * argv[]) {

int age = 10;

1. Creation of strings

NSString *s1 = @ "as God name";

NSString *s2 = [[NSString alloc] initwithstring:@ "Welcome to learn OC"]; Not used

NSString *S3 = [[NSString alloc] initwithformat:@ "Welcome to learn oc, age =%d", age]; Not used

NSString *S4 = [NSString stringwithformat:@ ' Welcome to learn oc, age =%d ', age];

C String---OC string

NSString *S5 = [[NSString alloc] initwithformat:@ "Welcome to learn OC"];

OC string---C string

const char *S5 = [S4 utf8string];

Reading a string from a file

NSString *S6 = [NSString stringwithcontentsoffile:@ "/users/apple/desktop/one.txt" encoding:nsutf8stringencoding Error:nil];

Read the URL string

Nsurl *url = [[Nsurl alloc] initwithstring:@ "File:///Users/apple/Desktop/one.txt"]; Creating a URL Path

Nsurl *URL2 = [Nsurl fileurlwithpath:@ "/users/apple/desktop/one.txt"]; Creating a URL Path

Reading a string from the URL path

NSString *S7 = [[NSString alloc] initwithcontentsofurl:url2 encoding:nsutf8stringencoding Error:nil];

NSLog (@ "%@", S7);

Writing a string to a file

NSString *S8 = @ "Welcome to learn OC";

Nsurl *URL3 = [[Nsurl alloc] initwithstring:@ "File:///Users/apple/Desktop/three.txt"];

[S8 writetofile:url3 atomically:yes encoding:nsutf8stringencoding Error:nil];

Nsmutablestring

nsmutablestring *S10 = [nsmutablestring stringwithformat:@ "Welcome to learn OC"];

[S10 appendstring:@ "Welcome to learn iOS development"]; Stitching the contents to the back of the S1

NSString *S11 = [NSString stringwithformat:@ "Welcome to my Blog"];

NSString *s12 = [S2 stringbyappendstring:@ "and other ..."]; Add a string after S11

Get string length
Nsuinteger length = Str.length
Gets the character of a position in a string
Unichar c = [str characteratindex:1];

return 0;

}

The NSString of Oc--foundation

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.