1. 程式人生 > >將手機號中間四位隱藏

將手機號中間四位隱藏

由於維護使用者個人隱私,我們會在使用者登陸後,將使用者名稱為電話號碼的進行區域性隱藏,解決思路,判斷暱稱是否符合電話號碼的正則表示式,符合就替換區域性字元。

  <textarea readonly="readonly" name="code" class="Objective-C"> 
NSString *phoneStr = @"13255670078";
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:phoneStr];
    if (isMatch) {
        NSMutableString *str = [[NSMutableString alloc]initWithString:nickName];
        [str replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        self.phoneLabel.text = str;
    }
<textarea readonly="readonly" name="code" class="Objective-C">
</textarea>