0

我在数据库中有大量的 NSStrings,它们被传递给 iOS 应用程序中的视图控制器。它们被格式化为“这是一条带有 $specially formatted$ 内容的消息”。

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

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

谢谢!

4

5 回答 5

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如果您想创建一个新字符串而不是修改原始字符串,还有一种情况。

于 2012-12-07T13:31:28.860 回答
1

我认为replaceOccurrencesOfString:这行不通,因为您有 start$ 和 end$。

但是,如果您将字符串与您分开,[string componentsSeperatedByString:@"$"]则会得到一个子字符串数组,因此每隔一个字符串就是您的“$specially formatted$”-string

于 2012-12-07T13:08:20.597 回答
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);

享受编程!

于 2012-12-07T13:09:28.663 回答
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."
于 2012-12-07T13:16:37.377 回答
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
于 2012-12-07T13:35:52.417 回答