OC Basic knowledge--string processing method

Source: Internet
Author: User

Immutable string

"Initialization of OC string"

1. Initialization of strings (subtraction method)

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

2. Initialization of strings (plus method)

NSString * STR3 = [NSString string];

3. Initialization of a string is given by another string

NSString * STR4 = [[NSString ALLOC]INITWITHSTRING:STR1];

4. Give the C-language string The OC string as the initialized value (minus method)

NSString * STR6 = [[NSString alloc]initwithutf8string: "CCCC"];

5. Give the C-language string to the OC string as the initialized value (plus method)

NSString * STR7 = [NSString stringwithutf8string: "CCCCC"];

6. The OC string initialization in a custom format (minus method)

NSString * Str8 = [[NSString alloc]initwithformat:@ "%d%c%@", 2, ' A ', @ "ohhhh~"];

7. The OC string initialization in a custom format (plus method)

NSString * STR9 = [NSString stringwithformat:@ "%d%c%@", 2, ' B ', @ "shabi~~"];

"Turn OC string to other type"

1. Convert string to int type

int i = [str intvalue];

2. Turn string into float type

float f = [str floatvalue];

3. Convert a string to a long long type

Long long L = [str longlongvalue];

4. Convert string to OC Nsuinteger type

Nsuinteger n = [str integervalue];

5. Convert a string to a double type

Double d = [str doublevalue];

6. Convert string to BOOL type

BOOL b = [str boolvalue];

7. Convert a string to a C-language string

const char * p = [str utf8string];

"Comparison of Strings"

1. Compare characters are not the same

BOOL ret = [str1 ISEQUALTOSTRING:STR2];

2. Compare the size of two strings

Nscomparisonresult Ret1 = [str1 compare:str2];

return value:

Nsorderedsame: Equal

Nsordereddescending: Descending (left greater than right)

Nsorderedascending: Ascending (right greater than left)

3. Get the length of the string

Nsuinteger len = [str1 length];

4. Get the first few characters of the OC string

Unichar C = [str1 characteratindex:0];

Traverse:

for (int i = 0; i < len; i + +) {

c = [str1 characteratindex:i];

NSLog (@ "%c", c);

}

"Sub-string"

1. Find the range of substrings in the parent string

Nsrange rang = [str rangeofstring:substr];

/*

typedef struct _NSRANGE {

Nsuinteger location; Index

Nsuinteger length; Length

} Nsrange;

*/

2. Extracting substrings (from index to end of string)

NSString * sub1 = [str substringfromindex:3];

3. Extracting substrings (from the beginning to the index excluding indexes)

NSString * sub2 = [str substringtoindex:3];

4. Extracting a range of substrings

SUB3 = [Str substringwithrange:nsmakerange (1, 4)];

"Case Conversion"

1. Lowercase all turn into uppercase

NSString * St2 = [St1 uppercasestring];

2. Uppercase all to lowercase

NSString * ST3 = [St1 lowercasestring];

"Mutable string"

"Initialize"

1. Initialization of open space (subtraction method)

nsmutablestring * str1 = [[nsmutablestring alloc]initwithcapacity:10];

2. Initialization of open space (plus method)

STR1 = [nsmutablestring stringwithcapacity:10];

3. Initialization of formatted strings

nsmutablestring * str2 = [[nsmutablestring alloc]initwithformat:@ "%@", @ "abc"];

"Variable string additions and deletions" no return value

1. Append

[str2 appendstring:@ "123"];

2. Formatted append

[str2 appendformat:@ "%d%@", 2,@ "ASD"];

3. Remove a range character from a string

[Str2 deletecharactersinrange:nsmakerange (0, 2)];

4. Insert

[str2 insertstring:@ "AAA" atindex:3];

5. Modify

[Str2 Replacecharactersinrange:nsmakerange (3, 4) withstring:@ "Maoge"];

6. Reset

[str2 setstring:@ "ABCD"];

3 Kinds of empty methods delete reset substitution

[Str2 deletecharactersinrange:nsmakerange (0, [str2 length])];

[str2 setstring:@ ""];

[Str2 replacecharactersinrange:nsmakerange (0, [str2 length]) withstring:@ ""];

OC Basic knowledge--string processing method

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.