使用NSScanner解析字符串 [英] Using NSScanner to parse a string

查看:113
本文介绍了使用NSScanner解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有格式标签的字符串,例如有{adult}成人和{children}孩子。我有一个字典,其中成人和孩子作为键,我需要查找值并用该值替换宏。这是完全动态的;键可以是任何东西(所以我不能硬编码 stringByReplacingString )。

I have a string with formatting tags in it, such as There are {adults} adults, and {children} children. I have a dictionary which has "adults" and "children" as keys, and I need to look up the value and replace the macros with that value. This is fully dynamic; the keys could be anything (so I can't hardcode a stringByReplacingString).

过去,我'之前通过循环遍历可变字符串并搜索字符来完成类似的事情;在我去的时候从源字符串中删除我已经搜索过的内容。看起来这正是NSScanner的设计类型,所以我尝试了这个:

In the past, I've done similar things before just by looping through a mutable string, and searching for the characters; removing what I've already searched for from the source string as I go. It seems like this is exactly the type of thing NSScanner is designed for, so I tried this:

NSScanner *scanner = [NSScanner scannerWithString:format];
NSString *foundString;
scanner.charactersToBeSkipped = nil;

NSMutableString *formatedResponse = [NSMutableString string];

while ([scanner scanUpToString:@"{" intoString:&foundString]) {
    [formatedResponse appendString:[foundString stringByReplacingOccurrencesOfString:@"{" withString:@""]]; //Formatted string contains everything up to the {

    [scanner scanUpToString:@"}" intoString:&foundString];

    NSString *key = [foundString stringByReplacingOccurrencesOfString:@"}" withString:@""];

    [formatedResponse appendString:[data objectForKey:key]];

}

NSRange range = [format rangeOfString:@"}" options:NSBackwardsSearch];

if (range.location != NSNotFound) {
    [formatedResponse appendString:[format substringFromIndex:range.location + 1]];
}

这个问题是当我的字符串以{开头时,那么扫描仪返回NO,而不是YES。 (这是文档所说的应该发生的事情)。我是否滥用NSScanner? scanUpToString 这一事实不包括作为其输出的一部分被搜索的字符串似乎使它几乎无用......

The problem with this is that when my string starts with "{", then the scanner returns NO, instead of YES. (Which is what the documentation says should happen). So am I misusing NSScanner? The fact that scanUpToString doesn't include the string that was being searched for as part of its output seems to make it almost useless...

可以轻松更改以执行我想要的操作,还是需要使用可变字符串重新编写并手动搜索字符?

推荐答案

使用 isAtEnd 确定何时停止。此外, {} 未包含在 scanUpToString的结果中:,因此它们将位于下一个字符串的开头,但是在循环之后的追加是没有必要的,因为即使找不到搜索字符串,扫描程序也会返回扫描的内容。

Use isAtEnd to determine when to stop. Also, the { and } are not included in the result of scanUpToString:, so they will be at the beginning of the next string, but the append after the loop is not necessary since the scanner will return scanned content even if the search string is not found.

// Prevent scanner from ignoring whitespace between formats. For example, without this, "{a} {b}" and "{a}{b}" and "{a}     
//{b}" are all equivalent
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@""]];
while(![scanner isAtEnd]) {
    if([scanner scanUpToString:@"{" intoString:&foundString]) {
        [formattedResponse appendString:foundString];
    }
    if(![scanner isAtEnd]) {
        [scanner scanString:@"{" intoString:nil];
        foundString = @""; // scanUpToString doesn't modify foundString if no characters are scanned
        [scanner scanUpToString:@"}" intoString:&foundString];
        [formattedResponse appendString:[data objectForKey:foundString];
        [scanner scanString:@"}"];
    }
}

这篇关于使用NSScanner解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆