NSString processing tips: Splitting strings

Source: Internet
Author: User

abstract The string type is one of the most used types in objective-c, and sometimes there are characters in the string that we do not want. such as "Hello World" in the space, or "Hello/world" in the "/", or "Hello a hello" in the ' a '. These can all be solved by means of nsstring.  

A string with a node, such as @ "<p> annoying nodes <br/></p>" We just want the Middle Chinese

Treatment Method One:

NSString *string1 =@ "<p> annoying nodes <br/></p>";/* Place the unwanted characters all in characterSet1, no additional commas or spaces, unless there is a space in the string you want to remove, here < P/etc are all alone, not as the entire character */Nscharacterset *characterset1 = [Nscharacterset charactersetwithcharactersinstring:@ "<p/brh>"]; //string1 by CHARACTERSET1 elements in the array nsarray *array1 = [string1 componentsseparatedbycharactersinset: CHARACTERSET1]; NSLog (@ "array =%@", array1); for (nsstring *string1 in array1) { if ([string1 length]>0) { //Here string is the Chinese string NSLog ( c13>@ "string =%@", string1); } }

Print Result: 2013-05-31 10:55:34.017 string[17634:303]

array = (
    "",
    "",
    "",
"\u8ba8\u538c\u7684\u8282\u70b9",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
)
2013-05-31 10:55:34.049 string[17634:303]
string = Nasty node

Two, a string with spaces, such as

@ "Hello World" remove space

NSString *string2 =@ "Hello World";                / * Handling spaces */nscharacterset *characterset2 = [Nscharacterset whitespacecharacterset]; //string1 by CHARACTERSET1 elements in the array 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);             

Printing results:

2013-05-31 11:02:49.656 string[17889:303]
Array = (
Hello
World
)
2013-05-31 11:02:49.657 string[17889:303] newstring = HelloWorld

PS: Handling other elements such as letters simply changes the value of the Nscharacterset.

+ (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;            

NSString processing tips: Splitting strings

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.