add PhoneUtil

This commit is contained in:
Looly 2020-07-30 08:59:29 +08:00
parent e36d687db2
commit f349d770e2
5 changed files with 149 additions and 186 deletions

View File

@ -3,10 +3,11 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
## 5.3.11 (2020-07-25) ## 5.3.11 (2020-07-30)
### 新特性 ### 新特性
* 【captcha】 AbstractCaptcha增加getImageBase64Data方法pr#985@Github * 【captcha】 AbstractCaptcha增加getImageBase64Data方法pr#985@Github
* 【core 】 增加PhoneUtilpr#990@Github
### Bug修复 ### Bug修复

View File

@ -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位身份证号码 * 18位身份证号码
*/ */

View File

@ -1,5 +1,6 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.lang.Validator; import cn.hutool.core.lang.Validator;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -9,21 +10,14 @@ import java.util.regex.Pattern;
* 手机号工具类 * 手机号工具类
* *
* @author dahuoyzs * @author dahuoyzs
* @since 5.3.11
*/ */
public class PhoneUtil { 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 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 * @since 5.3.11
*/ */
public static boolean isMobile(CharSequence value) { 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 * @since 5.3.11
*/ */
public static boolean isPhone(CharSequence value) { public static boolean isPhone(CharSequence value) {
return Validator.isMatchRegex(PHONE, value); return isMobile(value) || isTel(value);
} }
/** /**
@ -66,8 +60,8 @@ public class PhoneUtil {
* @return 替换后的字符串 * @return 替换后的字符串
* @since 5.3.11 * @since 5.3.11
*/ */
public static CharSequence hideBefore(CharSequence phone){ public static CharSequence hideBefore(CharSequence phone) {
return StrUtil.hide(phone,0,7); return StrUtil.hide(phone, 0, 7);
} }
/** /**
@ -77,8 +71,8 @@ public class PhoneUtil {
* @return 替换后的字符串 * @return 替换后的字符串
* @since 5.3.11 * @since 5.3.11
*/ */
public static CharSequence hideBetween(CharSequence phone){ public static CharSequence hideBetween(CharSequence phone) {
return StrUtil.hide(phone,3,7); return StrUtil.hide(phone, 3, 7);
} }
/** /**
@ -88,8 +82,8 @@ public class PhoneUtil {
* @return 替换后的字符串 * @return 替换后的字符串
* @since 5.3.11 * @since 5.3.11
*/ */
public static CharSequence hideAfter(CharSequence phone){ public static CharSequence hideAfter(CharSequence phone) {
return StrUtil.hide(phone,7,11); return StrUtil.hide(phone, 7, 11);
} }
/** /**
@ -99,8 +93,8 @@ public class PhoneUtil {
* @return 手机号前3位 * @return 手机号前3位
* @since 5.3.11 * @since 5.3.11
*/ */
public static CharSequence subBefore(CharSequence phone){ public static CharSequence subBefore(CharSequence phone) {
return StrUtil.sub(phone,0,3); return StrUtil.sub(phone, 0, 3);
} }
/** /**
@ -110,8 +104,8 @@ public class PhoneUtil {
* @return 手机号中间4位 * @return 手机号中间4位
* @since 5.3.11 * @since 5.3.11
*/ */
public static CharSequence subBetween(CharSequence phone){ public static CharSequence subBetween(CharSequence phone) {
return StrUtil.sub(phone,3,7); return StrUtil.sub(phone, 3, 7);
} }
/** /**
@ -121,8 +115,8 @@ public class PhoneUtil {
* @return 手机号后4位 * @return 手机号后4位
* @since 5.3.11 * @since 5.3.11
*/ */
public static CharSequence subAfter(CharSequence phone){ public static CharSequence subAfter(CharSequence phone) {
return StrUtil.sub(phone,7,11); return StrUtil.sub(phone, 7, 11);
} }
} }

View File

@ -4043,32 +4043,6 @@ public class StrUtil {
return replace(str, startInclude, endExclude, '*'); 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> * 替换字符字符数组中所有的字符为replacedStr<br>
* 提供的chars为所有需要被替换的字符例如"\r\n""\r""\n"都会被替换哪怕他们单独存在 * 提供的chars为所有需要被替换的字符例如"\r\n""\r""\n"都会被替换哪怕他们单独存在

View File

@ -9,12 +9,11 @@ import java.util.ArrayList;
* {@link PhoneUtil} 单元测试类 * {@link PhoneUtil} 单元测试类
* *
* @author dahuoyzs * @author dahuoyzs
*
*/ */
public class PhoneUtilTest { public class PhoneUtilTest {
@Test @Test
public void testCheck(){ public void testCheck() {
String mobile = "13612345678"; String mobile = "13612345678";
String tel = "010-88993108"; String tel = "010-88993108";
String errMobile = "136123456781"; String errMobile = "136123456781";
@ -32,7 +31,7 @@ public class PhoneUtilTest {
} }
@Test @Test
public void testTel(){ public void testTel() {
ArrayList<String> tels = new ArrayList<>(); ArrayList<String> tels = new ArrayList<>();
tels.add("010-12345678"); tels.add("010-12345678");
tels.add("020-9999999"); tels.add("020-9999999");
@ -51,24 +50,19 @@ public class PhoneUtilTest {
} }
@Test @Test
public void testHide(){ public void testHide() {
String mobile = "13612345678"; String mobile = "13612345678";
String hideBefore = "*******5678";
String hideBetween = "136****5678"; Assert.assertEquals("*******5678", PhoneUtil.hideBefore(mobile));
String hideAfter = "1361234****"; Assert.assertEquals("136****5678", PhoneUtil.hideBetween(mobile));
Assert.assertEquals(PhoneUtil.hideBefore(mobile),hideBefore); Assert.assertEquals("1361234****", PhoneUtil.hideAfter(mobile));
Assert.assertEquals(PhoneUtil.hideBetween(mobile),hideBetween);
Assert.assertEquals(PhoneUtil.hideAfter(mobile),hideAfter);
} }
@Test @Test
public void testSubString(){ public void testSubString() {
String mobile = "13612345678"; String mobile = "13612345678";
String subBefore = "136"; Assert.assertEquals("136", PhoneUtil.subBefore(mobile));
String subBetween = "1234"; Assert.assertEquals("1234", PhoneUtil.subBetween(mobile));
String subAfter = "5678"; Assert.assertEquals("5678", PhoneUtil.subAfter(mobile));
Assert.assertEquals(PhoneUtil.subBefore(mobile),subBefore);
Assert.assertEquals(PhoneUtil.subBetween(mobile),subBetween);
Assert.assertEquals(PhoneUtil.subAfter(mobile),subAfter);
} }
} }