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."];
这篇关于用其他字符替换字符串中的字符(唯一的然后是通配符值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用其他字符替换字符串中的字符(唯一的然后是通配符值)
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
