iOS 開發中經常會遇到處理字符串的問題,對於一個字符串經常會遇見裡面包含重復的字符需要對重復的字符進行處理,下面的代碼就是對重復字符進行處理的操作,具體代碼如下所示:
/** * 返回重復字符的location * * @param text 初始化的字符串 * @param findText 查找的字符 * * @return 返回重復字符的location */ - (NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText { NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:20]; if (findText == nil && [findText isEqualToString:@""]) { return nil; } NSRange rang = [text rangeOfString:findText]; if (rang.location != NSNotFound && rang.length != 0) { [arrayRanges addObject:[NSNumber numberWithInteger:rang.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; }