Summary of methods for manipulating strings using the NSString class in Objective-c _ios

Source: Internet
Author: User

One, string cutting
1, with a node string, such as @ "<p> hate node <br/></p>" We just want the middle of the Chinese

Processing method:

Copy Code code as follows:

NSString *string1 = @ "<p> hate node <br/></p>";

/* Here will not want to put all the characters in the CharacterSet1, do not need another comma or a space, and so on, unless the string you want to remove the space, here < P/And so are separate, not as the entire character * *

Nscharacterset *characterset1 = [Nscharacterset charactersetwithcharactersinstring:@ "<p/brh>"];

Split the string1 by the elements in CharacterSet1

Nsarray *array1 = [string1 componentsseparatedbycharactersinset:characterset1];

NSLog (@ "array =%@", array1);

For (NSString *string1 in array1)
{
if ([string1 length]>0) {

Here string is the Chinese string

NSLog (@ "string =%@", string1);
}
}

Print results:

2016-01-17 10:55:34.017 string[17634:303] 
array = ("
 ",
 "", ""
 ,
 "\u8ba8\u538c\u7684\u8282\ U70b9 "," "
 ,
 " ",
 " ",
 " "," "",
 "",
 "
", "", "" "," "," "," "," 2016-01-17 10:55:34.049 string[17634:303] 
string = annoying node

2, with a space string, such as

@ "Hello World" remove space

Copy Code code as follows:

NSString *string2 = @ "Hello world";

/* Processing Space * *

Nscharacterset *characterset2 = [Nscharacterset whitespacecharacterset];

Split the string1 by the elements in CharacterSet1
Nsarray *array2 = [string2 Componentsseparatedbycharactersinset:characterset2];

NSLog (@ "\narray =%@", array2);

Used to store the processed string
nsmutablestring *newstring1 = [nsmutablestring string];

For (NSString *string in array1)
{
[NewString1 appendstring:string];
}
NSLog (@ "newstring =%@", newString1);

Print results:

2016-01-17 11:02:49.656 string[17889:303] 
array = (
 Hello,
 world
)
2016-01-17 11:02:49.657 string[17889:303] NewString = HelloWorld

PS: Processing letters and other elements can only change the value of the Nscharacterset.

Copy Code code as follows:

+ (ID) controlcharacterset;

+ (ID) whitespacecharacterset;

+ (ID) whitespaceandnewlinecharacterset;

+ (ID) decimaldigitcharacterset;

+ (ID) lettercharacterset;

+ (ID) lowercaselettercharacterset;

+ (ID) uppercaselettercharacterset;

+ (ID) nonbasecharacterset;

+ (ID) alphanumericcharacterset;

+ (ID) decomposablecharacterset;

+ (ID) illegalcharacterset;

+ (ID) punctuationcharacterset;

+ (ID) capitalizedlettercharacterset;

+ (ID) symbolcharacterset;

+ (ID) newlinecharacterset ns_available (10_5, 2_0);

+ (ID) Charactersetwithrange: (Nsrange) Arange;

+ (ID) charactersetwithcharactersinstring: (NSString *) astring;

+ (ID) charactersetwithbitmaprepresentation: (NSData *) data;

+ (ID) charactersetwithcontentsoffile: (NSString *) fName;

Two, the character will nsarray the elements in the mosaic

Copy Code code as follows:

Nsarray *array = [Nsarray arraywithobjects:@ "Hello", @ "World", nil];

If you want to use,: such as string concatenation, just the following @ "" space into @, or @ ":" Can
NSString *string = [Array componentsjoinedbystring:@ "];

NSLog (@ "string =%@", string);

Print results:

Hello World

Third, intercept the substring:

Here take time as an example, using NSDate to get to the current time, sometimes only need a date or only need time

1, from the beginning of the string to intercept to the specified position, such as

Copy Code code as follows:

Get to Current date time
NSDate *date = [NSDate Date];

Defines the date format, which does not focus on the discussion of NSDate, so it is not detailed and will be discussed in detail later.
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

Set Date format
[Dateformatter setdateformat:@ "Yyyy-mm-dd hh:mm"];

Convert Date to NSString type
NSString *string = [Dateformatter stringfromdate:date];
NSLog (@ "\ncurrent =%@", string);

Intercept Date Substringtoindex
NSString *currentdate = [string substringtoindex:10];

NSLog (@ "\ncurrentdate =%@", currentdate);

Print results:

Current = 2016-01-1711:12


currentdate = 2016-01-17

2. Extract the middle substring-substringwithrange

Copy Code code as follows:

Lunar Day of interception
NSString *currentmonthanddate = [String Substringwithrange:[nsmakerange (5, 5)]];

NSLog (@ "currentmonthanddate =%@", currentmonthanddate);

Print results:

Currentmonthanddate = 06-27

3, from a certain location to intercept-Substringfromindex

Copy Code code as follows:

Intercept Time Substringfromindex
NSString *currenttime = [string substringfromindex:11];

NSLog (@ "\ncurrenttime =%@", currenttime); \

Print results:

CurrentTime = 11:25

Iv. Comparing strings

Copy Code code as follows:

NSString *first = @ "string";
NSString *second = @ "String";

1, to determine whether two strings are the same-isequaltostring method
Copy Code code as follows:

BOOL isequal = [Isequaltostring:second];

NSLog (@ "Equal to second:%@", isequal);

Print results:

The Equal to second:0

2, compare method comparison string three values

Copy Code code as follows:

nsorderedsame//is the same
nsorderedascending//Ascending, in alphabetical order, greater than true
nsordereddescending//Descending, in alphabetical order, less than true

BOOL result = [Compare:sencond] = = Nsorderedsame;
NSLog (@ "result:%d", result);


Print results:

result:0 

Copy Code code as follows:

BOOL result = [Compare:second] = = nsorderedascending;
NSLog (@ "result:%d", result);


Print results:

result:0

Copy Code code as follows:

BOOL result = [Compare:second] = = nsordereddecending; NSLog (@ "result:%d", result);

Print results:

Result:1

3. Do not consider case comparison string

Copy Code code as follows:

BOOL result = [Compare:second
Options:nscaseinsensitivesearch | Nsnumericsearch] = = Nsorderedsame;
NSLog (@ "result:%d", result);

Print results:

Result:1

V. Changing the case of strings

Copy Code code as follows:

NSString *astring = @ "A String";
NSString *string = @ "string";
Capital
NSLog (@ "astring:%@", [astring uppercasestring]);
Lowercase
NSLog (@ "string:%@", [string lowercasestring]);
Case of First letter
NSLog (@ "string:%@", [string capitalizedstring]);

Print results:

Astring:a STRING

string:string

string:string

Searching for substrings in strings

Copy Code code as follows:

NSString *string1 = @ "This is a string";
NSString *string2 = @ "string";
Nsrange range = [string1 rangeofstring:string2];
Nsuinteger location = range.location;
Nsuinteger leight = range.length;
NSString *astring = [[NSString alloc] initwithstring:[nsstring stringwithformat:@ "Location:%li,leight:%li", Location, Leight]];
NSLog (@ "astring:%@", astring);
[Astring release];

Print results:

Astring:location:10,leight:6

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.