Rang in iOS development to get repeated characters in a string

Source: Internet
Author: User

IOS development often encounters problems dealing with strings, and for a string that is often met with duplicate characters that need to be processed for repeated characters, the following code is the one that handles the repeating character, as shown in the following code:

/** * Returns the location of the repeating character * * @param text-initialized String * @param findText Find the character * * @return return the repeating character of location */-(nsmutablear Ray *) GETRANGESTR: (NSString *) text findText: (NSString *) findtext{nsmutablearray *arrayranges = [Nsmutablearray arrayWi    THCAPACITY:20];    if (FindText = = Nil && [FindText isequaltostring:@ "]) {return nil;    } Nsrange rang = [text Rangeofstring:findtext]; if (rang.location! = Nsnotfound && Rang.length! = 0) {[Arrayranges addobject:[nsnumber numberwithinteger:r        Ang.location]];        Nsrange rang1 = {0,0};        Nsinteger location = 0;        Nsinteger length = 0;                for (int i = 0;; i++) {if (0 = = i) {location = rang.location + rang.length;                length = Text.length-rang.location-rang.length;            Rang1 = Nsmakerange (location, length);                }else {location = rang1.location + rang1.length; Length = Text.length-rang1.location-rang1.length;            Rang1 = Nsmakerange (location, length);            } rang1 = [text Rangeofstring:findtext options:nscaseinsensitivesearch range:rang1];            if (rang1.location = = Nsnotfound && Rang1.length = = 0) {break;        }else [Arrayranges addobject:[nsnumber numberWithInteger:rang1.location];    } return arrayranges; } return nil;}

The returned array object is the location of the repeating character in text and is used in a convenient form to get rang={array[i], findtext.length}

Rang in iOS development to get repeated characters in a string

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.