[英]iPhone : insert a character inside a dynamic string
I have a dynamic string suppose 我想有一个动态字符串
NSString *originalString = @"Hello my Phone : 123123123 abc 987";
In the above string first i have to check were numbers are present inside the string which i got it by doing below code :- 在上面的字符串中,首先我必须检查字符串中是否存在数字,我可以通过执行以下代码来获得它:-
NSString *newString = [[MessageStr componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
result i got in newString is 123123123987 我在newString中得到的结果是123123123987
But my problem is not getting out numbers from string, i just want to add a special character after every number inside the string which is dynamic. 但是我的问题不是从字符串中取出数字,我只想在字符串中的每个数字之后添加一个特殊字符,这是动态的。
like i want to make my string like this :- 就像我想让我的字符串这样:-
NSString *originalString = @"Hello my Phone : 1,2,3,1,2,3,1,2,3 abc 9,8,7";
So, please any one can suggest me, what's best possible way to achieve above string. 因此,请任何人可以建议我,实现上述字符串的最佳方法是什么。
Thanks in advance. 提前致谢。
I finally succeed by doing the following code :- 通过执行以下代码,我终于成功了:
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"([0-9])" options:0 error:NULL];
NSString *newString = [regexp stringByReplacingMatchesInString:MessageStr options:0 range:NSMakeRange(0, MessageStr.length) withTemplate:@"$0,"];
NSLog(@"Changed %@", newString);
Thanks @nsgulliver by posting answer in the link :- 感谢@nsgulliver通过在链接中发布答案:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.