替换NSString中的字符

ARC*_*ARC 10 macos xcode objective-c ios

我试图用*的替换字符串中除了最后4个之外的所有字符.
在objective-c中,在NSString类replaceStringWithCharactersInRange: withString:中有一个方法,我将(0,[string length]-4)使用string 给出它的范围@"*".这就是它的作用:123456789ABCD被修改为*ABCD,而我正在寻找制作********ABCD.我知道它替换了我用string对象指定的范围.怎么做到这一点?

Lef*_*ris 12

NSError *error;

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

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