iOS learning the use of the eighth article--nsstring

Source: Internet
Author: User

Common methods and use of iOS strings

NSString

1. NSString four-way method of creation

(1) NSString *str1 = @ "Way One";

(2) NSString *str2 = [[NSString alloc] initwithstring:@ "Way II"];

(3) NSString *STR3 = [NSString stringwithformat:@ "%@", @ "way three"];

(4) NSString *STR4 = [nsstring stringwihtutf8string: "mode four"];//converted from C string to OC string

2. NSString Writing Files

NSString *str = @ "Oh Oh";

NSString *path = @ "Users/zz/desktop/test.txt";//file does not exist automatically created, folder does not exist will error

Nserror *error;

[Str writetofile:path atomically:yes encoding:nsutf8stringencoding Error:&error];

if (Error) {

NSLog (@ "failed");

}else{

NSLog (@ "Success");

}

3. NSString Uppercase and lowercase conversions

Nssttring *string = @ "Qwer";

(1). Convert all lowercase

[String lowercasestring];

(2). Convert all Uppercase

[String uppercasestring];

(3). First letter uppercase, remaining lowercase

[String capitalizedstring];

4. NSString character Comparison

NSString *str1 = @ "Qwer";

NSString *str2 = @ "Qwer";

(1). Isequaltostring to determine whether two strings are equal

[Str1 ISQUALTOSTRING:STR2];

(2). Compare comparison

The return type of compare is a cocoa data type called Nscomparisonresult, with three values: nsordererascending,nsorderedsame,nsordereddescending. Corresponding to each other- 1,0,1

Uppercase letters less than lowercase letters

5. NSString Search String rangofstring

NSString *str = @ "Qwer";

if ([str rangofstring:@ "w"! = Nsnotfount]) {

NSLog (@ "w");

}

6. NSString Intercept String

NSString *str = @ "qwerty";

(1) Substringtoindex intercept the string before the subscript

STR1 = [str substringtoindex:2];//intercept subscript 2 before the string str1 = @ "QW";

(2) Substringfromindex intercept the string after the subscript

STR2 = [str substringfromindex:2];//intercept subscript 2 after the string str2 = @ "ert";

(3). Substringwithrang Intercept A string

Nsrang rang = Nsmakerang (1,3);//1 subscript, 3 length

STR2 = [Str substringwihtrang:rang];//intercept starting from subscript 1, length 3 string str3 = Wer

7. Substitution of nsstring strings

NSString *str = @ "qwerty";

(1). Replace all specified characters stringbyreplacingoccurrencesofstring:@ "a" withstring:@ "B" to change all a in the string to B. A can be a character or multiple characters.

STR1 = [str stringbyreplacingoccurrencesofstring:@ "W" withstring:@ "o"];//result str1 = @ "Qoerty";

(2). Replace the character at the specified position stringbyreplacingcharactersinrange:nsrange withstring @ "B". Nsrange is the place to replace, B is the string to replace

Nsrange rang = Nsmakerang (1,3);

STR2 = [str stringbyreplacingcharactersinrange:rang withstring:@ "BB"];//result str2 = @ "Qbbty";

8. NSString clear whitespace from the end of the string

NSString *str = @ "qwerty";

(1). Clear spaces by System method

STR1 = [str stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset];

Result str1 = @ "Qwery";

(2). Clear spaces by alternate method

STR2 = [STR [email protected] "" Withstring @ ""];

Result str2 = @ "Qwery";

9. Conversion of NSString and basic data types

(1). Nsdictionary conversion into NSString

Nsarray *array = [Nsarray arraywithobjcts:@ "We", @ "users", nil];

NSString *str = [Array [email protected] ","]; "," means separated by commas

Result: str = @ "We,users";

(2). NSData conversion into NSString

NSData *data = [[NSData alloc] init];//Assuming that data is a value

NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

(3). String to int

NSString *str = @ "123";

int intstring = [str intvalue];

(4). int to String

int i = 2;

NSString *str = [NSString stringwithformat:@ "%d", I];

(5). String to float

NSString *str = @ "123";

float f = [str floatvalue];

(6). Float to String

float f = 23.4;

NSString *str = [NSString stringwithformat:@ "%f", f];

Division of NSString Strings

NSString *str = @ "1,2,3,4";

Nsarray *array = [str componentsseparatedbystring:@ ","];

To split str into an array.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS learning the use of the eighth article--nsstring

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.