[英]Replacing instances of a character with two different characters in Objective-C
I have a huge amount of NSStrings in a database that get passed to a view controller in an iOS app. 我在数据库中有大量NSString,这些NSString传递给iOS应用程序中的视图控制器。 They are formatted as "This is a message with $specially formatted$ content".
它们的格式为“这是具有$特殊格式的$内容的消息”。
However, I need to change the '$' at the start of the special formatting with a '[' and the '$' at the end with ']'. 但是,我需要在特殊格式的开头更改为'[',在结尾更改为']'时更改'$'。 I have a feeling I can use an NSScanner but so far all of my attempts have produced wackily concatenated strings!
我有一种可以使用NSScanner的感觉,但是到目前为止,我所有的尝试都产生了奇怪的串联字符串!
Is there a simple way to recognise a substring encapsulated by '$' and swap them out with start/end characters? 有没有一种简单的方法可以识别由'$'封装的子字符串并将其替换为开始/结束字符? Please note that a lot of the NSStrings have multiple '$' substrings.
请注意,许多NSString具有多个“ $”子字符串。
Thanks! 谢谢!
You can use regular expressions: 您可以使用正则表达式:
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].
The pattern @"\\\\$([^$]*)\\\\$"
searches for 模式
@"\\\\$([^$]*)\\\\$"
搜索
$<zero_or_more_characters_which_are_not_a_dollarsign>$
and all occurrences are then replaced by [...]
. 然后将所有出现的内容替换为
[...]
。 The pattern contains so many backslashes because the $
must be escaped in the regular expression pattern. 该模式包含许多反斜杠,因为
$
必须在正则表达式模式中转义。
There is also stringByReplacingMatchesInString
if you want to create a new string instead of modifying the original string. 如果要创建新字符串而不是修改原始字符串,则还有
stringByReplacingMatchesInString
。
I think replaceOccurrencesOfString:
won't work cause you have start$ and end$. 我认为
replaceOccurrencesOfString:
无法工作,因为您有start $和end $。
But if you seperate the Strings with [string componentsSeperatedByString:@"$"]
you get an Array of substrings, so every second string is your "$specially formatted$"-string 但是,如果用
[string componentsSeperatedByString:@"$"]
分隔字符串,则会得到一个子字符串数组,因此第二个字符串就是您的“ $特殊格式的$”字符串
This should work! 这应该工作!
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);
Enjoy Programming! 享受编程!
// 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."
Try this one 试试这个
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);
Output: 输出:
-->This is a message with [specially formatted] content. This is a message with [specially formatted] content
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.