iOS Stage Learning 13th day notes (NSString and nsmutablestring)

Source: Internet
Author: User

iOS Learning (OC language) Knowledge Point finishing

Operation of the OC string

1) There are two types of strings in OC:

1, immutable string NSString: Cannot modify object contents, but can change pointer of object.

2, variable string nsmutablestring: Can modify the object content.

Second, NSString immutable string operation

1) Assign a string constant object directly to the string reference NSString *[email protected] "Hello"; The output format of the string object: NSLog (@ "str1=%@", str1).

2) initwithstring can construct string object in OC to reference NSString *str2=[[nsstring ALLOC]INITWITHSTRING:STR1];.

3) initwithutf8string can create an OC string object for the C language string, converting the C string to an OC string: NSString *str3=[[nsstring alloc]initwithutf8string: "IOS"];

4) Initwithformat can create the OC string Object int age=20 with the format of OC string;

NSString *str4=[[nsstring alloc]initwithformat:@ "name is%@,age is%d", str1,age];

5) Use the. Length method to get the length of the string Nsuinteger len= str1.length;

6) Characteratindex can take out a word according to subscript such as: nsstring *[email protected] "Hello"; Unichar c= [str1 characteratindex:0]; The result is: H

7) Compare is used to compare two strings, the method is case-sensitive, and the return result is Nscomparisonresult enumerated type data enumeration value has

1. nsorderedascending indicates that the previous string is less than the next string

2. Nsorderedsame means two strings are equal

3. Nsordereddescending indicates that the previous string is greater than the last string

Instance code:

1NSString *str1=@"Hello";2NSString *str3=[[nsstring alloc]initwithutf8string:"IOS"];3Nscomparisonresult cmp=[str1 COMPARE:STR3];4  if(cmp==nsorderedascending) {5NSLog (@"%@<%@", STR1,STR3);6}Else if(cmp==nsorderedsame) {7NSLog (@"%@=%@", STR1,STR3);8}Else if(cmp==nsordereddescending) {9NSLog (@"%@>%@", STR1,STR3);Ten}//Results: Hello<ios

8) Caseinsensitivecompare case-insensitive comparison strings; compare strings, you can set comparison options

Nsnumericsearch: If there are numbers in the string, Compare by number size, for example: Ios5<iso12

Nscaseinsensitivesearch: Case-insensitive comparisons, such as Ios7=ios7

Instance code:

1Nscomparisonresult cmp=[str1 CASEINSENSITIVECOMPARE:STR3];2str1=@"iOS7";3str3=@"iOS7";4cmp=[str1 compare:str3 options:nscaseinsensitivesearch];5 if(cmp==nsorderedascending) {6NSLog (@"%@<%@", STR1,STR3);7}Else if(cmp==nsorderedsame) {8NSLog (@"%@=%@", STR1,STR3);9}Else if(cmp==nsordereddescending) {TenNSLog (@"%@>%@", STR1,STR3); One } results: Ios7=ios7

9) isequaltostring Compare 2 string contents for equality, return bool (yes/no) value

Instance code:

 1  nsstring *str1=@ " hello   ;  2  nsstring *str2=[[nsstring Alloc] INITWITHSTRING:STR1];  3   ([ Str1 Isequaltostring:str2] { 4  NSLog (@ "  %@ equal%@   " ,STR1,STR2);  5 } Result: Hello equal hello  

Uppercasestring the lowercase letter string to uppercase NSString *[email protected] "Hello"; NSString *str6=[str1 uppercasestring];

The result is: HELLO

One ) lowercasestring the uppercase string to lowercase.

Hasprefix determines whether to start with a string (as a prefix) for example:

NSString [email protected] "www.baidu.com";

if ([str1 hasprefix:@ "www"]) {

NSLog (@ "yes");

Result: Yes

Hassuffix determines whether to end with a string (suffix) for example:

NSString [email protected] "www.baidu.com";

if ([str1 hassuffix:@ "com"]) {

NSLog (@ "yes");

Result: Yes

() substringfromindex start with a sub-string (to the end) from a subscript such as:

NSString [email protected] "This is string A";

NSString *res=[str1 Substringfromindex:1]; Result: He is string A

() Substringtoindex from the first character to a substring of the underlying string, excluding the character corresponding to the current number, and taking the preceding character in the current number subscript

For example:

NSString [email protected] "This is string A";

NSString *res=[str1 Substringtoindex:2]; Results: Th

[[str1 Substringfromindex:8] substringtoindex:5]; results: Strin

Substringwithrange) returns substrings by range

Nsrange range={8,5};//direct to range value

Range=nsmakerange (8, 5);//Returns a range of variables by function

NSString [email protected] "This is string A";

NSString Res=[str1 Substringwithrange:range]; Results: Strin

rangeofstring) finds substrings in a string and returns the position and length of the string Nsrange

For example:

NSString [email protected] "This is string A";

Nsrange range=[str1 rangeofstring:@ "string"]; Result: {8,6}

if (range.location!=nsnotfound) {

NSLog (@ "find");

Result: Find

three, nsmutablestring variable string operation

1) initwithstring create variable string objects with immutable strings for example:

NSString *[email protected] "This is string B";

Nsmutablestring *mstr=[[nsmutablestring ALLOC]INITWITHSTRING:STR];

2) insertstring inserts a string at the specified subscript location for example:

Nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@ "This is String B"];

[Mstr insertstring:@ "Hello" atindex:0]; Result: Hello this is string B

3) AppendString appends strings to strings such as:

Nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@ "This is String B"];

[MSTR appendstring:@ "Shanghai"]; Result: This is string B Shanghai

4) AppendFormat appends a formatted string to a string such as:

Nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@ "This is String B"];

[Mstr appendformat:@ "%d", 20]; Result: This is string B 20

5) Deletecharactersinrange Deletes a string of the specified range for example:

Nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@ "This is String B"];

[Mstr deletecharactersinrange:nsmakerange (0, 6)]; Result: s string B

6) Replacecharactersinrange replaces the string of the specified range with a new string, for example:

1Nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@"This is string B"];2[Mstr Replacecharactersinrange:nsmakerange (2,2) Withstring:@" is"];//Result: This is string B3  //Replace All is in the string with is4Nsrange range=[mstr rangeofstring:@" is"];5   while(range.location!=nsnotfound) {6[Mstr Replacecharactersinrange:range withstring:@" is"];7Range=[mstr rangeofstring:@" is"];8}

7) Replaceoccurrencesofstring replaces all the is in the specified range in the string with is, for example:

Nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@ "This is String B"];

[Mstr replaceoccurrencesofstring:@ "is" withstring:@ "is" Options:1 range:nsmakerange (0, mstr.length)];

Result: This is string B

8) setString Modify string Contents For example: nsmutablestring *mstr=[[nsmutablestring alloc]initwithstring:@ "This is String B"];

[mstr setstring:@ "Hello"]; Result: Hello

iOS Stage Learning 13th day notes (NSString and nsmutablestring)

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.