我有一个大的 NSString 对象,如下所示。我想解析这个字符串以获取其中的所有 icmp_seq 和时间值。我写的代码总是给我最后的价值。
任何想法如何以更好的方式做到这一点,除了用换行符分割它,然后在每次分割时运行解析器。
64 bytes from 74.125.129.105: icmp_seq=0 ttl=43 time=23.274 ms
64 bytes from 74.125.129.105: icmp_seq=1 ttl=43 time=28.704 ms
64 bytes from 74.125.129.105: icmp_seq=2 ttl=43 time=23.519 ms
64 bytes from 74.125.129.105: icmp_seq=3 ttl=43 time=23.548 ms
64 bytes from 74.125.129.105: icmp_seq=4 ttl=43 time=23.517 ms
64 bytes from 74.125.129.105: icmp_seq=5 ttl=43 time=23.293 ms
64 bytes from 74.125.129.105: icmp_seq=6 ttl=43 time=23.464 ms
64 bytes from 74.125.129.105: icmp_seq=7 ttl=43 time=23.323 ms
64 bytes from 74.125.129.105: icmp_seq=8 ttl=43 time=23.451 ms
64 bytes from 74.125.129.105: icmp_seq=9 ttl=43 time=23.560 ms
代码:
-(void)parsePingData:(NSString *)iData {
NSRange anIcmpRange = [iData rangeOfString:@"icmp_seq"];
NSRange aTtlRange =[iData rangeOfString:@"ttl"];
NSRange icmpDataRange = NSMakeRange(anIcmpRange.location + 1, aTtlRange.location - (anIcmpRange.location + 1));
NSLog(@"Output=%@",[iData substringWithRange:icmpDataRange]);
}