1、第一种封装方法
- (NSString *)hidePartWithStr:(NSString *)Str holderSingleStr:(NSString *)holderSingleStr location:(NSInteger)location length:(NSInteger)length
{
NSString *hideNumStr = Str;
NSString *placeHolderStr = [NSString string];
if ([Str isNotBlank] && [holderSingleStr isNotBlank])
{
for (int i=0; i<length; i++)
{
placeHolderStr = [placeHolderStr stringByAppendingString:holderSingleStr];
}
hideNumStr = [Str stringByReplacingCharactersInRange:NSMakeRange(location, length) withString:placeHolderStr];
}
return hideNumStr;
}
2、第二种简单方法,NSRang 获取索引为3长度为4的范围,然后使用字符串替换方法,把这个范围内的字符串替换成自定的字符串,次案例为四个星号@“****”
NSRange range = NSMakeRange(3, 4);
NSString *userPhone = [user.user_phone stringByReplacingCharactersInRange:range withString:@"****"];