替换与其他字符的字符串的字符(唯一那么通配符值) [英] Replace characters (unique then wildcard value) in a string with other characters

查看:292
本文介绍了替换与其他字符的字符串的字符(唯一那么通配符值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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."

推荐答案

使用一个普通的前pression。搜索模式将是:

Use a regular expression. The search pattern would be:

,(.)

替换模式将是:

,$1.

样code:

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."];

这篇关于替换与其他字符的字符串的字符(唯一那么通配符值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆