把中间的字符串替换成等长度的*
2018-06-27 本文已影响0人
再也不要见
一、比如12345678900----------->想显示成123*****900
1、使用
NSString *name=@"12345678900";
NSString *phoneStr= [self replaceStringWithAsterisk:name startLocation:3 lenght:name.length -6];
2、字符串、开始位置、结束位置
-(NSString *)replaceStringWithAsterisk:(NSString *)originalStr startLocation:(NSInteger)startLocation lenght:(NSInteger)lenght
{
NSString *newStr = originalStr;
for (int i = 0; i < lenght; i++) {
NSRange range = NSMakeRange(startLocation, 1);
newStr = [newStr stringByReplacingCharactersInRange:range withString:@"*"];
startLocation ++;
}
return newStr;
}