美文网首页
iOS 遍历字符串中某两个字符之间的所有字符串

iOS 遍历字符串中某两个字符之间的所有字符串

作者: orilme | 来源:发表于2017-03-13 23:46 被阅读149次

    有时候后台传过来的数据要截取出来分别展示,记录一下

    
    NSString *dataStr =@"北戴河泽田旅馆位于北戴河海滨的中心赤土山村,奥林匹克大道公园斜对面,购物、用餐、出行都十分便利。\n- 距离北戴河火车站约9.5公里;\n- 距离秦皇岛站约18.9公里;\n- 距离秦皇岛机场约29.3公里。";
        NSString *temp = nil;
        NSMutableArray *array = [[NSMutableArray alloc]init];
        NSMutableArray *array2 = [[NSMutableArray alloc]init];
        for(int i =0; i < [dataStr length]-1; i++) {
            
            temp = [dataStr substringWithRange:NSMakeRange(i,2)];
            if ([temp isEqualToString:@"- "]) {
                [array addObject:[NSString stringWithFormat:@"%d", i]];
            }
            
            if ([temp isEqualToString:@"公里"]) {
                [array2 addObject:[NSString stringWithFormat:@"%d", i]];
            }
            
        }
        NSMutableArray *locationArray = [[NSMutableArray alloc]init];
        NSMutableArray *distanceArray = [[NSMutableArray alloc]init];
        for (NSInteger i=0; i<array.count; i++) {
            NSString *string  = [dataStr substringWithRange:NSMakeRange([array[i] integerValue], [array2[i] integerValue] - [array[i] integerValue])];
            NSRange startRange = [string rangeOfString:@"距离"];
            NSRange endRange = [string rangeOfString:@"约"];
            NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
            NSString *result = [string substringWithRange:range];
            [locationArray addObject:result];
            
            NSRange startRange2 = [string rangeOfString:@"约"];
            NSRange endRange2 = [string rangeOfString:@"公里"];
            NSRange range2 = NSMakeRange(startRange2.location + startRange2.length, endRange2.location - startRange2.location - startRange2.length);
            NSString *result2 = [string substringWithRange:range2];
            [distanceArray addObject:result2];
            
        }
    
    

    相关文章

      网友评论

          本文标题:iOS 遍历字符串中某两个字符之间的所有字符串

          本文链接:https://www.haomeiwen.com/subject/qlqcnttx.html