//查找文本內容裏包含的手機號,並將中間四位替換爲****
+ (NSString *)getHiddenPhoneNumberContentTextUseString:(NSString *)originContentText;
{
NSError *error;
//設置正則規則:第一位是數字1,接下來後邊是10位數字
NSRegularExpression *attachmentExpression = [NSRegularExpression regularExpressionWithPattern:@"1[\\d]{10}"
options:NSRegularExpressionCaseInsensitive error:&error];
//獲取符合條件的結果數組
NSArray *resultArr = [attachmentExpression matchesInString:originContentText options:0 range:NSMakeRange(0, originContentText.length)];
if (resultArr.count > 0) {
//遍歷結果數組,替換對應位置的字符串
for (NSTextCheckingResult *tmpResult in resultArr) {
NSString *tmpStr = [originContentText substringWithRange:tmpResult.range];
originContentText = [originContentText stringByReplacingCharactersInRange:tmpResult.range withString:[NSString getHiddenPhoneNumberUseString:tmpStr]];
}
}
return originContentText;
}
//替換手機號中間四位爲****
+ (NSString *)getHiddenPhoneNumberUseString:(NSString *)originPhoneNumber
{
//校驗長度,防止傳入數據不對引起崩潰
if (originPhoneNumber.length >= 7) {
return [originPhoneNumber stringByReplacingOccurrencesOfString:[originPhoneNumber substringWithRange:NSMakeRange(3,4)] withString:@"****"];
} else {
return originPhoneNumber;
}
}