iOS隐藏部分邮箱账号

给别人写了个隐藏部分邮箱账号的代码...帖一下,顺便记录下以备后用.

显示前两位和@符号前两位这4位,中间的隐藏.剩下的都显示.

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString * emailStr = @"[email protected]";//邮箱号
    [self hiddenEmailNum:emailStr];
}

- (NSString *)hiddenEmailNum:(NSString *)EmailStr
{
    NSString *symbolStr = @"******************";
    NSString * lastStr =  @"@";//截取符
    NSRange rangeLenth = [EmailStr rangeOfString:lastStr];
    //开始
    NSRange rangeBegin = NSMakeRange(0, 2);
    NSString *beginStr = [EmailStr substringWithRange:rangeBegin];
    
    //隐藏部分
    NSRange rangeHidden = NSMakeRange(2, rangeLenth.location - 4);
    NSString * hiddenStr = [EmailStr substringWithRange:rangeHidden];
    
    //替换隐藏部分
    NSRange rangSymbol = NSMakeRange(0, hiddenStr.length);
    NSString *newHiddenStr = [symbolStr substringWithRange:rangSymbol];
    
    //结尾
    NSRange rangeEnd = NSMakeRange(rangeLenth.location - 2, EmailStr.length - rangeLenth.location + 2);
    NSString *endStr = [EmailStr substringWithRange:rangeEnd];
    
    NSString * newStr = [NSString stringWithFormat:@"%@%@%@",beginStr,newHiddenStr,endStr];
    
    return newStr;
}

 

转载于:https://my.oschina.net/u/2555813/blog/757645

你可能感兴趣的:(iOS隐藏部分邮箱账号)