Replace characters (unique then wildcard value) in a string with other characters(用其他字符替换字符串中的字符(唯一的然后是通配符值))
问题描述
我有一个 NSString,比如说 "H,L,K,P" 我怎样才能检测到一个特定的字符而不是一个通配符...例如,检查 ",*" 将返回 ",L" ",K" 和 ",P" 因为它们都有特定的 ",",然后它们后面都有一个字符.然后我想用它自己加上一个句点"替换那个字符串.
I have an NSString, let's say "H,L,K,P" how can I detect a specific character than then a wild-car character... for example, checking for ",*" would return ",L" ",K" and ",P" because they all have the specific "," and then they all have a character after them. Then I want to replace that string with itself plus a "period" appended to it.
所以H,L,K,P"会变成H,L.,K.,P"
So "H,L,K,P" would become "H,L.,K.,P."
推荐答案
使用正则表达式.搜索模式是:
Use a regular expression. The search pattern would be:
,(.)
替换模式是:
,$1.
示例代码:
NSString *string = @"H,L,K,P";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@",(.)"
options:0
error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:@",$1."];
这篇关于用其他字符替换字符串中的字符(唯一的然后是通配符值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!