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

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

问题描述

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

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

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