Last active
October 23, 2019 06:15
-
-
Save keepingcoding/e19b05c36162301ea78b02858d710c2b to your computer and use it in GitHub Desktop.
字符工具类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.util.StringUtils; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class StringUtil { | |
private StringUtil() { | |
} | |
/** | |
* 判断字符串是否包含字母 | |
* | |
* @param str | |
* @return | |
*/ | |
public static boolean containsLetter(String str) { | |
if (!StringUtils.hasText(str)) { | |
return false; | |
} | |
String regex = ".*[a-zA-Z]+.*"; | |
Matcher m = Pattern.compile(regex).matcher(str); | |
return m.matches(); | |
} | |
/** | |
* 判断给定字符是不是中文 | |
* GENERAL_PUNCTUATION 判断中文的“号 | |
* CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号 | |
* HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号 | |
* @param c | |
* @return | |
*/ | |
public static boolean isChinese(char c) { | |
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); | |
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS | |
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS | |
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | |
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION | |
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION | |
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* 判断给定字符串是否包含中文 | |
* | |
* @param strName | |
* @return | |
*/ | |
public static boolean isChinese(String strName) { | |
char[] ch = strName.toCharArray(); | |
for (char c : ch) { | |
if (isChinese(c)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment