Iphone, about string segmentation

Source: Internet
Author: User

I am a beginner in iphone, and I have not learned many things, and many operations on strings are irrelevant and I do not know how to operate them.

Similar to "Rose | rose.jpg, Lily | xx.jpg, sunflower | xxx.jpg", how to obtain classification? 1) rose Lily, sunflower, 1w.rose.jpg, xx.jpg, xxx, and jpg. You can use two strings to separate them.

The following is a simple example: Obtain the number NSString * phone = "021-387126654,0579-02369865,0571-867545"; or NSString * phone = "021-387126654 ";

1) use the more complex nsange:

NSMutableString * muPhone = [[NSMutableString alloc] initWithString: phone];

Nsange range = [muPhone rangeOfString: @ ","];

Int location = range. location;

Int length = range. length;

// When you still need to judge that there is only one phone number, that is, no. When multiple phone numbers are separated, this method will fail if you do not make the following judgment.

If (0 = length ){

Self. rPhone = phone;

} Else {

Self. rPhone = [phone substringToIndex: location];

}

2) use the string segmentation method (this method does not need to be like 1) and need to be judged ):

NSArray * phones = [phone componentsSeparatedByString: @ ""]; // ** string segmentation **

// You only need to determine if phones. count is> 0. Obtain the following 1st phone numbers.

Self. rPhone = [phones objectAtIndex: 0];

// Obtain multiple phone numbers

For (NSString * p in phones ){

Self. rPhone = p;

}

You can use a similar method to obtain the desired string from a string.

Below are some of the links that I have found by my predecessors to better summarize string operations. I would like to make a memo here for the future:

Http://xiaozu.renren.com/xiaozu/218897/333605171 Iphone array some basic operations NSArray/NSMutableArray;

Http://www.wangdg.com /? P = 123 Summary of NSString processing;

Search string truncation extraction for http://blog.csdn.net/houchangxi/article/details/6760158 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.