iOS Development--string segmentation, stitching

Source: Internet
Author: User

---------------------A string split instance---------------------
NSString * ptr = @ "I am a Man";


Return a string as a whole as a split condition to a nsarray non-variable group
Nsarray * array = [ptr componentsseparatedbystring:@ ""];


If modified, convert Nsarray to Nsmutablearray
Nsmutablearray * array1 = [Nsmutablearray Arraywitharray:array];


To split a character in a string as a segmentation condition
Nsarray * array2 = [ptr componentsseparatedbycharactersinset:[nscharacterset charactersetwithcharactersinstring:@ "," ]];

"Character Set"
Nscharacterset This is a character set.


Converts a string into a character set
[Nscharacterset charactersetwithcharactersinstring:@ ","]


---------------------string Concatenation instance---------------------
NSString * str = [array componentsjoinedbystring:@ ""];
If the split condition appears at the beginning or the end, an empty string @ "" will appear, and if not, it will need to be converted to nsmutablestring to process the empty string.
FUNC1: [Array1 removeobject:@ ""];
Find empty string to delete directly
Func2:for (id obj in array1) {
if ([obj length] = = 0)
The length of the empty string is 0
if ([obj isequaltostring:@ ""])
Compare to empty string (string is not = = comparison, use function)
}

In OC, there are 3 ways to say,

NSString *str=[nsstring    initwithformat:@ "%@,%@", a  , b]; NSString *str=[a   stringbyappendingstring:b]; NSString *str=[string   stringbyappendingformat:@ "%@,%@",  a  ,  b];

iOS Development--string segmentation, stitching

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.