objective-c - 替换 NSString 中的字符

标签 objective-c ios xcode macos

我正在尝试用 * 替换字符串中除最后 4 个字符之外的所有字符。
在 objective-c 中,NSString 类 replaceStringWithCharactersInRange: withString: 中有一个方法,我会给它范围 (0,[string length]-4) ) with string @"*"。这就是它的作用:123456789ABCD 被修改为 *ABCD,而我正在寻找制作 ********ABCD。 我知道它用字符串对象替换了我指定的范围。如何实现?

最佳答案

NSError *error;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\d" options:NSRegularExpressionCaseInsensitive error:&error];

NSString *newString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"*"];

关于objective-c - 替换 NSString 中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7650516/

相关文章:

iphone - ! vs == nil 在 objective-c

ios - 使用不同的类和键对数组进行排序

ios - 在调用 UITableViewController 之前准备好节数和行数

ios - 以编程方式更改 UIButton 的标题在 iOS Xcode-Beta 3 中不起作用

ios - 多个 UIView 动画

iphone - 当 alertview 显示时不要连接 segue

架构 x86_64 的 IOS undefined symbol

ios - 如何在重新使用 MKAnnotationView 时为它的外观设置动画

xcode - 是否可以在 Xcode 8/Swift 3 Beta for iOS 9 中发布应用程序?

ios - 输入字符串或字符时应用程序崩溃