Objective-c String Notes

Source: Internet
Author: User

String

string divided into variable string and immutable string

how immutable strings are initialized

NSString *string = [[NSString alloc] init];

NSString *string = [NSString string];

string is not a container class cannot add data

NSString *string = [[NSString alloc] initwithstring:string];

NSString *string = @ "132586";

string Processing

1. format processing of strings

int Age = ;

nsstring *name = @ " positive ";

nsstring *info = [nsstring stringwithformat:@ "%@ this year %d ", name, age ]; // formatted string

The meaning of this statement is to initialize info to "20 years of age."

1. from which location to begin intercepting a string

nsstring *newstring = [info substringfromindex:2]; This number is not subscript .

This statement is meant to "be 20 years of age" in the beginning of the second character to intercept characters into newstring so that the value in NewString is "20 years old this year"

2. Where to intercept the string

NSLog(@ "%@", [Message substringtoindex:5]);

3. Where to intercept the string from

Nsrange Range;

Range. location = 6;

Range. length = 6;

NSLog(@ "%@", [Message substringwithrange: Range]);

Note: The rang in this method is a struct of the Nsrange type, all to instantiate a struct and assign a value

splits a string into an array by a specified character

nsarray *array = [Message componentsseparatedbystring:@ ","];

This means that the value in the message is separated by "," as a delimiter, the value in the message is divided into several parts, and then saved to the array

convert English to uppercase

nsstring *myname = @ "liyongjun250";

NSLog(@ "%@", MyName. Uppercasestring);

It means to output the characters in MyName in uppercase.

Convert all English letters to lowercase

NSLog(@ "%@", MyName. Uppercasestring. LowerCaseString);

This means that the characters in the myname are output in lowercase

Capitalize first letter

NSLog(@ "%@", MyName. Capitalizedstring);

It means that the characters in the MyName are capitalized in the first letter,

Stitching strings

NSString *pinjie = [s stringbyappendingstring: ss];

Add the value of SS to the back of S

NSString *pinjie1 = [s stringbyappendingformat:@ "%@ no I am bad ", SS];

It means putting the values of SS in front of "No I am bad" and stitching these characters behind s

[Result containsstring:@ " hit Tiger "]! = NO

Determine if result contains "Play Tiger"

[Result Hassuffix:@ "ya~"]! = NO

Determine if result ends with "ya~"

Variable string

[SSA replacecharactersinrange: Ang withstring:@ " Good Man "];

It means to replace the Ang with a "Good Man", where Ang is a nsrange type structure. The first element is the location where the length is.

[Name1 appendString:@ "Sdfals"];

It means "sdfals" is stitched to the back of the name1 character.

Objective-c String Notes

Related Article

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.