在Objective-C中用两个不同的字符替换字符的实例

时间:2012-12-07 12:54:52

标签: iphone objective-c ios nsstring nsscanner

我在数据库中有大量的NSStrings,它们被传递到iOS应用程序中的视图控制器。它们被格式化为“这是带有特殊格式$ content的消息”。

但是,我需要在特殊格式的开头更改'$',并在'['和'''结尾处加'$'。我有一种感觉,我可以使用NSScanner,但到目前为止,我所有的尝试都产生了令人讨厌的串联字符串!

是否有一种简单的方法来识别由'$'封装的子字符串并将它们与开始/结束字符交换出来?请注意,很多NSStrings都有多个'$'子串。

谢谢!

5 个答案:

答案 0 :(得分:2)

您可以使用正则表达式:

NSMutableString *str = [@"Hello $World$, foo $bar$." mutableCopy];

NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"\\$([^$]*)\\$"
                                                  options:0
                                                    error:NULL];
[regex replaceMatchesInString:str
                      options:0
                        range:NSMakeRange(0, [str length])
                 withTemplate:@"[$1]"];
NSLog(@"%@", str);
// Output:
// Hello [World], foo [bar].

模式@"\\$([^$]*)\\$"搜索

$<zero_or_more_characters_which_are_not_a_dollarsign>$

然后所有出现的事件都被[...]替换。该模式包含许多反斜杠,因为$必须以正则表达式模式进行转义。

如果您想创建一个新字符串而不是修改原始字符串,还有stringByReplacingMatchesInString

答案 1 :(得分:1)

我认为replaceOccurrencesOfString:不起作用,因为你已经开始$并结束$。

但是如果你用[string componentsSeperatedByString:@"$"]分隔字符串你会得到一个子字符串数组,所以每隔一个字符串就是你的“$ special formatted formatted” - 字符串

答案 2 :(得分:1)

这应该有效!

NSString *str = @"This is a message with $specially formatted$ content";
NSString *original = @"$";
NSString *replacement1 = @"[";
NSString *replacement2 = @"]";

BOOL start = YES;
NSRange rOriginal = [str rangeOfString: original];
while (NSNotFound != rOriginal.location) {
    str = [str stringByReplacingCharactersInRange: rOriginal withString:(start?replacement1:replacement2)];
    start = !start;
    rOriginal = [str rangeOfString: original];
}

NSLog(@"%@", str);

享受编程!

答案 3 :(得分:1)

// string = @"This is a $special markup$ sentence."
NSArray *components = [string componentsSeparatedByString:@"$"];
// sanity checks
if (components.count < 2) return; // maybe no $ characters found
if (components.count % 2) return; // not an even number of $s
NSMutableString *out = [NSMutableString string];
for (int i=0; i< components.count; i++) {
   [out appendString:components[i]];
   [out appendString: (i % 2) ? @"]" : @"[" ];
}
// out = @"This is a [special markup] sentence."

答案 4 :(得分:0)

试试这个

NSMutableString *string=[[NSMutableString alloc]initWithString:@"This is a message with $specially formatted$ content. This is a message with $specially formatted$ content"];


NSMutableString *string=[[NSMutableString alloc]initWithString:@"This is a message with $specially formatted$ content. This is a message with $specially formatted$ content"];

BOOL open=YES;
for (NSUInteger i=0; i<[string length];i++) {
    if ([string characterAtIndex:i]=='$') {
        if (open) {
            [string replaceCharactersInRange:NSMakeRange(i, 1) withString:@"["];
            open=!open;
        }
        else{
            [string replaceCharactersInRange:NSMakeRange(i, 1) withString:@"]"];
            open=!open;
        }
    }
}
NSLog(@"-->%@",string);

输出:

-->This is a message with [specially formatted] content. This is a message with [specially formatted] content