iOS , 使用 rangeOfString 或 NSmutablearray 读取 NSString 的指定部分
iOS , Read specified part of NSString with rangeOfString or NSmutablearray
我在NSString
这样我想只读(我在这里)这部分我怎么办,我在这里是一个变量
[[["OK'","ok'","","OK'",0]],,"AB",,[["hello'",[1],YES,YES,100,0,4,0]],[["ok '",1,[["okidoki '",100,yes,yes]],[[0,3]],"okdoki'"]],,[,"I am here",[4]],[["AB"]],3]
试试这个代码。
使用正则表达式,我们解析 [] 中的子字符串。拆分字符串后,
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\[(.*?)\]" options:NSRegularExpressionCaseInsensitive error:NULL];
NSString *test = @"[[[\"OK\",\"ok\",\"\",\"OK\",0]],,\"AB\",,[[\"hello\",[1],YES,YES,100,0,4,0]],[[\"ok\",1,[[\"okidoki\",100,yes,yes]],[[0,3]],\"okdoki\"]],,[,\"I am here\",[4]],[[\"AB\"]],3]";
NSString *input = test;
NSArray *myArray = [regex matchesInString:input options:0 range:NSMakeRange(0, [input length])] ;
NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];
NSRange matchRange = [myArray[4] rangeAtIndex:1];
[matches addObject:[input substringWithRange:matchRange]];
NSString *elementString = [matches lastObject];
NSArray *listItems = [elementString componentsSeparatedByString:@","];
NSLog(@"I'm here: %@", listItems[1]);
我在NSString
这样我想只读(我在这里)这部分我怎么办,我在这里是一个变量
[[["OK'","ok'","","OK'",0]],,"AB",,[["hello'",[1],YES,YES,100,0,4,0]],[["ok '",1,[["okidoki '",100,yes,yes]],[[0,3]],"okdoki'"]],,[,"I am here",[4]],[["AB"]],3]
试试这个代码。 使用正则表达式,我们解析 [] 中的子字符串。拆分字符串后,
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\[(.*?)\]" options:NSRegularExpressionCaseInsensitive error:NULL];
NSString *test = @"[[[\"OK\",\"ok\",\"\",\"OK\",0]],,\"AB\",,[[\"hello\",[1],YES,YES,100,0,4,0]],[[\"ok\",1,[[\"okidoki\",100,yes,yes]],[[0,3]],\"okdoki\"]],,[,\"I am here\",[4]],[[\"AB\"]],3]";
NSString *input = test;
NSArray *myArray = [regex matchesInString:input options:0 range:NSMakeRange(0, [input length])] ;
NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];
NSRange matchRange = [myArray[4] rangeAtIndex:1];
[matches addObject:[input substringWithRange:matchRange]];
NSString *elementString = [matches lastObject];
NSArray *listItems = [elementString componentsSeparatedByString:@","];
NSLog(@"I'm here: %@", listItems[1]);