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