替换 NSString 中引号之间的逗号的方法,请检查我的一个

发布于 2024-11-26 06:52:25 字数 1308 浏览 1 评论 0原文

出于解析目的,有必要将配额内的逗号替换为空格。我在 NSString 类中没有找到解决方案并编写了这个。 它有效,但我想知道您的意见是否有更简单的方法:

- (NSString *) replaceBetweenQuotesInString:(NSString*)line {

    const char *buffer = [line UTF8String];

    NSMutableIndexSet *evenIndexes = [NSMutableIndexSet indexSet];    

    NSMutableIndexSet *oddIndexes = [NSMutableIndexSet indexSet];

    BOOL evenOdd = YES;

    for (unsigned int i = 0; i < [line length]; i++) {


        if (buffer[i] == '"'){
            if (evenOdd)
                [evenIndexes addIndex:i];
            else
                [oddIndexes addIndex:i];

        evenOdd = !evenOdd;
        }
    }

    if ([evenIndexes count] != [oddIndexes count] )
        [evenIndexes removeIndex:[evenIndexes lastIndex]];

    int totalRanges = (int) [evenIndexes count];

    for (int i = 0; i < totalRanges; i++) {

        NSRange range = NSMakeRange([evenIndexes firstIndex], [oddIndexes firstIndex] - [evenIndexes firstIndex]);
        [evenIndexes removeIndex:[evenIndexes firstIndex]];
        [oddIndexes removeIndex:[oddIndexes firstIndex]];
        line = [line stringByReplacingOccurrencesOfString:@"," withString:@" " options:NSLiteralSearch range:range];

    }

    [evenIndexes removeAllIndexes];
    [oddIndexes removeAllIndexes];    

    return line;

}

For parsing purposes it was necessary to replace commas inside quotas with space. I did not find and solution in NSString class and wrote this one.
It works, but I would like to know your opinion if there is more simple approach:

- (NSString *) replaceBetweenQuotesInString:(NSString*)line {

    const char *buffer = [line UTF8String];

    NSMutableIndexSet *evenIndexes = [NSMutableIndexSet indexSet];    

    NSMutableIndexSet *oddIndexes = [NSMutableIndexSet indexSet];

    BOOL evenOdd = YES;

    for (unsigned int i = 0; i < [line length]; i++) {


        if (buffer[i] == '"'){
            if (evenOdd)
                [evenIndexes addIndex:i];
            else
                [oddIndexes addIndex:i];

        evenOdd = !evenOdd;
        }
    }

    if ([evenIndexes count] != [oddIndexes count] )
        [evenIndexes removeIndex:[evenIndexes lastIndex]];

    int totalRanges = (int) [evenIndexes count];

    for (int i = 0; i < totalRanges; i++) {

        NSRange range = NSMakeRange([evenIndexes firstIndex], [oddIndexes firstIndex] - [evenIndexes firstIndex]);
        [evenIndexes removeIndex:[evenIndexes firstIndex]];
        [oddIndexes removeIndex:[oddIndexes firstIndex]];
        line = [line stringByReplacingOccurrencesOfString:@"," withString:@" " options:NSLiteralSearch range:range];

    }

    [evenIndexes removeAllIndexes];
    [oddIndexes removeAllIndexes];    

    return line;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-12-03 06:52:25

我还没有对此进行测试,但因为您似乎并不关心转义引号:

NSMutableString* result = [[NSMutableString alloc] init];
NSArray* components = [line componentsSeparatedByString: @"\""];
for (int i = 0 ; i < [components count] ; ++i)
{
    if (i % 2 == 0)
    {
        [result appendString: [components objectAtIndex: i]];
    }
    else
    {
        NSString* fixed = [[components objectAtIndex: i] stringByReplacingOccurrencesOfString: @"," 
                                                                                   withString: @" "];
        [result appendFormat: @"\"%@\"", fixed];
    }
}

I have not tested this but since you do not seem to be concerned about escaped quotes:

NSMutableString* result = [[NSMutableString alloc] init];
NSArray* components = [line componentsSeparatedByString: @"\""];
for (int i = 0 ; i < [components count] ; ++i)
{
    if (i % 2 == 0)
    {
        [result appendString: [components objectAtIndex: i]];
    }
    else
    {
        NSString* fixed = [[components objectAtIndex: i] stringByReplacingOccurrencesOfString: @"," 
                                                                                   withString: @" "];
        [result appendFormat: @"\"%@\"", fixed];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文