字符串分析,用逗号分隔,除非用撇号将其括起来 [英] string parsing, separate by comma, unless it is enclosed by apostrophes
问题描述
我需要在iOS应用的Objective-C中解析以下字符串
I need to parse the following string in Objective-C for iOS app
NSString * htmlString = @"12,22,'stringA','','stringB,stringC',2,'stringD'";
NSString *htmlString = @"12, 22, 'stringA','', 'stringB, stringC', 2,'stringD'";
我想要一个这样的数组
{
@12,
@22,
@"stringA",
@"emptySlotInfo",
@"stringB, stringC",
@2,
@"stringD"
}
头痛是@"strinb,stringC",因为
The headache is @"strinb, stringC" because
[htmlString componentsSeparatedByString:@","];
在这种情况下不起作用,并且@'"作为分隔符也不起作用.
does not work for the case and the @"'" as separator does not work either.
如何获取必要的组件?
推荐答案
您可以使用如果它扫描'
,则它知道一个字符串正在开始,并且忽略,
,直到读取下一个'
.如果未读取任何开头'
,则按,
分隔.
If it scans a '
, it knows a string is starting and ignore ,
till it reads the next '
. if no opening '
was read, sperate by ,
.
这篇cocoawithlove文章可能会有所帮助
我做了一个快速的原型.很可能还有很多要优化的地方,因为我也不是NSScanner的专家
I made a quick prototype. Most likely there is much to optimize, as I am also not a expert for 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"];
}];
resultArray包含
The resultArray contains
(
12,
22,
stringA,
emptySlotInfo,
stringB, stringC,
2,
stringD
)
这篇关于字符串分析,用逗号分隔,除非用撇号将其括起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!