繁体   English   中英

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

[英]Replacing instances of a character with two different characters in Objective-C

我在数据库中有大量NSString,这些NSString传递给iOS应用程序中的视图控制器。 它们的格式为“这是具有$特殊格式的$内容的消息”。

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

有没有一种简单的方法可以识别由'$'封装的子字符串并将其替换为开始/结束字符? 请注意,许多NSString具有多个“ $”子字符串。

谢谢!

您可以使用正则表达式:

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

我认为replaceOccurrencesOfString:无法工作,因为您有start $和end $。

但是,如果用[string componentsSeperatedByString:@"$"]分隔字符串,则会得到一个子字符串数组,因此第二个字符串就是您的“ $特殊格式的$”字符串

这应该工作!

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);

享受编程!

// 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."

试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM