xcode 替换 NSString 中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7650516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace characters in NSString
提问by ARC
I am trying to replace all characters except last 4 in a String with *'s.
In objective-c there is a method in NSString class replaceStringWithCharactersInRange: withString:where I would give it range (0,[string length]-4)) with string @"*". This is what it does: 123456789ABCD is modified to *ABCD while I am looking to make ********ABCD.
I understand that it replaced range I specified with string object. How to accomplish this ?
我试图用 * 替换字符串中除最后 4 个字符以外的所有字符。
在objective-c 中有一个在NSString 类replaceStringWithCharactersInRange: withString:中的方法,我会给它 range (0,[string length]-4)) 和 string @"*"。这就是它的作用:123456789ABCD 被修改为 *ABCD,而我正在寻找 ********ABCD。我知道它替换了我用字符串对象指定的范围。如何做到这一点?
回答by Lefteris
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\d" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *newString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"*"];
回答by ARC
This looks like a simple problem... get the first part string and return it with the last four characters appended to it.
Here is a function that returns the needed string :
这看起来像一个简单的问题......获取第一部分字符串并返回它并附加最后四个字符。
这是一个返回所需字符串的函数:
-(NSString *)neededStringWithString:(NSString *)aString {
// if the string has less than or 4 characters, return nil
if([aString length] <= 4) {
return nil;
}
NSUInteger countOfCharToReplace = [aString length] - 4;
NSString *firstPart = @"*";
while(--countOfCharToReplace) {
firstPart = [firstPart stringByAppendingString:@"*"];
}
// range for the last four
NSRange lastFourRange = NSMakeRange([aString length] - 4, 4);
// return the combined string
return [firstPart stringByAppendingString:
[aString substringWithRange:lastFourRange]];
}
回答by Chuck
The most unintuitive part in Cocoa is creating the repeating stars without some kind of awkward looping. stringByPaddingToLength:withString:startingAtIndex:allows you to create a repeating string of any length you like, so once you have that, here's a simple solution:
Cocoa 中最不直观的部分是在没有某种笨拙循环的情况下创建重复的星星。stringByPaddingToLength:withString:startingAtIndex:允许您创建任意长度的重复字符串,因此一旦有了它,这是一个简单的解决方案:
NSInteger starUpTo = [string length] - 4;
if (starUpTo > 0) {
NSString *stars = [@"" stringByPaddingToLength:starUpTo withString:@"*" startingAtIndex:0];
return [string stringByReplacingCharactersInRange:NSMakeRange(0, starUpTo) withString:stars];
} else {
return string;
}
回答by James Boutcher
I'm not sure why the accepted answer was accepted, since it only works if everything but last 4 is a digit. Here's a simple way:
我不确定为什么接受的答案被接受,因为它仅在除最后 4 外的所有内容都是数字时才有效。这是一个简单的方法:
NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"1234567890ABCD"];
NSRange r = NSMakeRange(0, [str1 length] - 4);
[str1 replaceCharactersInRange:r withString:[[NSString string] stringByPaddingToLength:r.length withString:@"*" startingAtIndex:0]];
NSLog(@"%@",str1);
回答by tilo
You could use [theString substringToIndex:[theString length]-4]to get the first part of the string and then combine [theString length]-4 *'s with the second part. Perhaps their is an easier way to do this..
您可以使用[theString substringToIndex:[theString length]-4]获取字符串的第一部分,然后将 [theString length]-4 * 与第二部分组合起来。也许他们是一个更简单的方法来做到这一点..
回答by Vitaliy
NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"1234567890ABCD"];
[str1 replaceCharactersInRange:NSMakeRange(0, [str1 length] - 4) withString:@"*"];
NSLog(@"%@",str1);
it works
有用
回答by CocoaChris
The regexp didn't work on iOS7, but perhaps this helps:
正则表达式在 iOS7 上不起作用,但也许这有帮助:
- (NSString *)encryptString:(NSString *)pass {
NSMutableString *secret = [NSMutableString new];
for (int i=0; i<[pass length]; i++) {
[secret appendString:@"*"];
}
return secret;
}
In your case you should stop replacing the last 4 characters. Bit crude, but gets the job done
在您的情况下,您应该停止替换最后 4 个字符。有点粗糙,但可以完成工作

