Objective c 字符串解析,用逗号分隔,除非用撇号括起来
我需要在Objective-C for iOS应用程序中解析以下字符串 NSString*htmlString=@“12,22,'stringA','','stringB,stringC',2,'stringD' 我想要一个这样的数组Objective c 字符串解析,用逗号分隔,除非用撇号括起来,objective-c,ios,cocoa-touch,parsing,Objective C,Ios,Cocoa Touch,Parsing,我需要在Objective-C for iOS应用程序中解析以下字符串 NSString*htmlString=@“12,22,'stringA','','stringB,stringC',2,'stringD' 我想要一个这样的数组 { @12, @22, @"stringA", @"emptySlotInfo", @"stringB, stringC", @2, @"stringD" } 头痛是"strinb,stringC",因为,
{
@12,
@22,
@"stringA",
@"emptySlotInfo",
@"stringB, stringC",
@2,
@"stringD"
}
头痛是"strinb,stringC",因为,
不适用于这种情况,@“'”作为分隔符也不起作用
如何获取必要的组件?您可以使用
如果它扫描一个,
,它知道一个字符串正在启动,并忽略,
,直到它读取下一个,
。如果未读取打开的,
,请按,
进行分类
可能会有帮助
我做了一个快速的原型。很可能还有很多需要优化的地方,因为我也不是NSScanner的专家
NSString *htmlString = @"12, 22, 'stringA','', 'stringB, stringC', 2,'stringD'";
NSScanner *scanner = [NSScanner scannerWithString:htmlString];
NSString *apostrophe = @"'"; // scanner needs to detect this
NSString *comma = @","; // scanner needs to detect this
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@%@", apostrophe, comma]];
BOOL apostropheOpen = NO; // is the scan location inside a single quoted substring?
NSInteger lastCommaIndex = -1; // track last found comma's index
NSMutableArray *array = [NSMutableArray array];
while (![scanner isAtEnd]) {
[scanner scanUpToCharactersFromSet:charSet intoString:NULL];
NSString *charAtlocation = [htmlString substringWithRange:NSMakeRange([scanner scanLocation], 1)];
if ([charAtlocation isEqualToString:apostrophe]){
apostropheOpen = !apostropheOpen;
} else if ([charAtlocation isEqualToString:comma]){
if (!apostropheOpen) {
[array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
lastCommaIndex = [scanner scanLocation];
}
}
[scanner setScanLocation:[scanner scanLocation]+1];
} ;
// scanner only dealt with the string until the last comma, probably one more value to handle
if (lastCommaIndex < [scanner scanLocation]){
[array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
}
// array contains seperated strings, but with blanks and apostrophes
// we will deal with them now
__block NSMutableArray *resultArray = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
obj = [[obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
stringByTrimmingCharactersInSet:charSet];
if ([obj length] > 0)
[resultArray addObject:obj];
else
[resultArray addObject:@"emptySlotInfo"];
}];
非常感谢。这似乎正是我需要的。学习NSScanner类的好案例。非常感谢。太好了。
NSString *htmlString = @"12, 22, 'stringA','', 'stringB, stringC', 2,'stringD'";
NSScanner *scanner = [NSScanner scannerWithString:htmlString];
NSString *apostrophe = @"'"; // scanner needs to detect this
NSString *comma = @","; // scanner needs to detect this
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@%@", apostrophe, comma]];
BOOL apostropheOpen = NO; // is the scan location inside a single quoted substring?
NSInteger lastCommaIndex = -1; // track last found comma's index
NSMutableArray *array = [NSMutableArray array];
while (![scanner isAtEnd]) {
[scanner scanUpToCharactersFromSet:charSet intoString:NULL];
NSString *charAtlocation = [htmlString substringWithRange:NSMakeRange([scanner scanLocation], 1)];
if ([charAtlocation isEqualToString:apostrophe]){
apostropheOpen = !apostropheOpen;
} else if ([charAtlocation isEqualToString:comma]){
if (!apostropheOpen) {
[array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
lastCommaIndex = [scanner scanLocation];
}
}
[scanner setScanLocation:[scanner scanLocation]+1];
} ;
// scanner only dealt with the string until the last comma, probably one more value to handle
if (lastCommaIndex < [scanner scanLocation]){
[array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
}
// array contains seperated strings, but with blanks and apostrophes
// we will deal with them now
__block NSMutableArray *resultArray = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
obj = [[obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
stringByTrimmingCharactersInSet:charSet];
if ([obj length] > 0)
[resultArray addObject:obj];
else
[resultArray addObject:@"emptySlotInfo"];
}];
(
12,
22,
stringA,
emptySlotInfo,
stringB, stringC,
2,
stringD
)