mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add PhoneUtil
This commit is contained in:
parent
e36d687db2
commit
f349d770e2
@ -3,10 +3,11 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
## 5.3.11 (2020-07-25)
|
||||
## 5.3.11 (2020-07-30)
|
||||
|
||||
### 新特性
|
||||
* 【captcha】 AbstractCaptcha增加getImageBase64Data方法(pr#985@Github)
|
||||
* 【core 】 增加PhoneUtil(pr#990@Github)
|
||||
|
||||
### Bug修复
|
||||
|
||||
|
@ -55,7 +55,7 @@ public class PatternPool {
|
||||
/**
|
||||
* 移动电话
|
||||
*/
|
||||
public final static Pattern MOBILE = Pattern.compile("(?:0|86|\\+86)?1[3456789]\\d{9}");
|
||||
public final static Pattern MOBILE = Pattern.compile("(?:0|86|\\+86)?1[3-9]\\d{9}");
|
||||
/**
|
||||
* 18位身份证号码
|
||||
*/
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.lang.PatternPool;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
@ -9,21 +10,14 @@ import java.util.regex.Pattern;
|
||||
* 手机号工具类
|
||||
*
|
||||
* @author dahuoyzs
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public class PhoneUtil {
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
*/
|
||||
private static Pattern MOBILE = Pattern.compile("(?:0|86|\\+86)?1[3456789]\\d{9}");
|
||||
/**
|
||||
* 座机号码
|
||||
* */
|
||||
*/
|
||||
private static Pattern TEL = Pattern.compile("0\\d{2,3}-[1-9]\\d{6,7}");
|
||||
/**
|
||||
* 座机号码+手机号码
|
||||
* */
|
||||
private static Pattern PHONE = Pattern.compile("^([0-9]{3}-?[0-9]{8})|dao([0-9]{4}-?[0-9]{7})$");
|
||||
|
||||
/**
|
||||
* 验证是否为手机号码(中国)
|
||||
@ -33,7 +27,7 @@ public class PhoneUtil {
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public static boolean isMobile(CharSequence value) {
|
||||
return Validator.isMatchRegex(MOBILE, value);
|
||||
return Validator.isMatchRegex(PatternPool.MOBILE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +49,7 @@ public class PhoneUtil {
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public static boolean isPhone(CharSequence value) {
|
||||
return Validator.isMatchRegex(PHONE, value);
|
||||
return isMobile(value) || isTel(value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4043,32 +4043,6 @@ public class StrUtil {
|
||||
return replace(str, startInclude, endExclude, '*');
|
||||
}
|
||||
|
||||
/**
|
||||
* 制定字符覆盖原字符串。
|
||||
* 注意参数:
|
||||
* StrUtil.hide()是 开始位置,到结束位置。
|
||||
* StrUtil.cover()是 开始位置,指定长度。
|
||||
*
|
||||
* @param str 原字符串
|
||||
* @param start 开始位置
|
||||
* @param len 覆盖的长度
|
||||
* @param character 覆盖的符号
|
||||
* @return 返回值类型 符号覆盖字符后的字符串
|
||||
* @since 5.3.11
|
||||
* @author dahuoyzs
|
||||
*/
|
||||
public CharSequence cover(String str,int start,int len,Character character){
|
||||
if (start<0||len>str.length()){
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
int end = start + len;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
sb.append((start <= i && i < end) ? character : str.charAt(i));
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换字符字符数组中所有的字符为replacedStr<br>
|
||||
* 提供的chars为所有需要被替换的字符,例如:"\r\n",则"\r"和"\n"都会被替换,哪怕他们单独存在
|
||||
|
@ -9,7 +9,6 @@ import java.util.ArrayList;
|
||||
* {@link PhoneUtil} 单元测试类
|
||||
*
|
||||
* @author dahuoyzs
|
||||
*
|
||||
*/
|
||||
public class PhoneUtilTest {
|
||||
|
||||
@ -53,22 +52,17 @@ public class PhoneUtilTest {
|
||||
@Test
|
||||
public void testHide() {
|
||||
String mobile = "13612345678";
|
||||
String hideBefore = "*******5678";
|
||||
String hideBetween = "136****5678";
|
||||
String hideAfter = "1361234****";
|
||||
Assert.assertEquals(PhoneUtil.hideBefore(mobile),hideBefore);
|
||||
Assert.assertEquals(PhoneUtil.hideBetween(mobile),hideBetween);
|
||||
Assert.assertEquals(PhoneUtil.hideAfter(mobile),hideAfter);
|
||||
|
||||
Assert.assertEquals("*******5678", PhoneUtil.hideBefore(mobile));
|
||||
Assert.assertEquals("136****5678", PhoneUtil.hideBetween(mobile));
|
||||
Assert.assertEquals("1361234****", PhoneUtil.hideAfter(mobile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubString() {
|
||||
String mobile = "13612345678";
|
||||
String subBefore = "136";
|
||||
String subBetween = "1234";
|
||||
String subAfter = "5678";
|
||||
Assert.assertEquals(PhoneUtil.subBefore(mobile),subBefore);
|
||||
Assert.assertEquals(PhoneUtil.subBetween(mobile),subBetween);
|
||||
Assert.assertEquals(PhoneUtil.subAfter(mobile),subAfter);
|
||||
Assert.assertEquals("136", PhoneUtil.subBefore(mobile));
|
||||
Assert.assertEquals("1234", PhoneUtil.subBetween(mobile));
|
||||
Assert.assertEquals("5678", PhoneUtil.subAfter(mobile));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user