
com.webapp.utils.string.Utils Maven / Gradle / Ivy
The newest version!
package com.webapp.utils.string;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import com.webapp.utils.jpinyin.PinyinFormat;
import com.webapp.utils.jpinyin.PinyinHelper;
import com.webapp.utils.regex.RegexConst;
/**
* 字符串相关便捷工具类
*/
public final class Utils {
private Utils(){}
public static interface Charsets {
public static final String uft8 = StandardCharsets.UTF_8.name();
public static final String gbk = "gbk";
public static final String iso = StandardCharsets.ISO_8859_1.name();
}
public static interface Symbol{
/** Enter - {@value} */
public static final String Enter = "\n";
/** Tab - {@value} */
public static final String Tab = "\t";
/** Empty - {@value} */
public static final String Empty = "";
/** Space - {@value} */
public static final String Space = " ";
/** Dot - {@value} */
public static final String Dot = ".";
/** Comma - {@value} */
public static final String Comma = ",";
/** Colon - {@value} */
public static final String Colon = ":";
/** Semicolon - {@value} */
public static final String Semicolon = ";";
/** LineThrough - {@value} */
public static final String LineThrough = "-";
/** Underline - {@value} */
public static final String LineUnder = "_";
/** Pound - {@value} */
public static final String Pound = "#";
/** Question - {@value} */
public static final String Question = "?";
/** And - {@value} */
public static final String And = "&";
/** Dollar - {@value} */
public static final String Dollar = "$";
/** Percent - {@value} */
public static final String Percent = "%";
/** At - {@value} */
public static final String At = "@";
/** Slash - {@value} */
public static final String Slash = "/";
/** Backslash - {@value} */
public static final String Backslash = "\\";
/** LParen - {@value} */
public static final String LParen = "(";
/** RParen - {@value} */
public static final String RParen = ")";
/** LBrace - {@value} */
public static final String LBrace = "{";
/** RBrace - {@value} */
public static final String RBrace = "}";
/** LBracket - {@value} */
public static final String LBracket = "[";
/** RBracket - {@value} */
public static final String RBracket = "]";
}
/*
* pinyin
* 中国 - zhongguo
* @param str
* @return pinyin
*/
public static String toPinyin(String str) {
return toPinyin(str, "");
}
/*
* pinyin
* 中国 - zhongguo
* @param str
* separator pinyin separator
* @return pinyin
*/
public static String toPinyin(String str, String separator) {
return PinyinHelper.convertToPinyinString(str, separator, PinyinFormat.WITHOUT_TONE);
}
/*
* ShortPinyin
* 中国 - zg
* @param str
* @return ShortPinyin
*/
public static String toShortPinyin(String str) {
return PinyinHelper.getShortPinyin(str);
}
/*
* Underline
* userId - user_id
* @param str
* @return Underline
*/
public static String toSnake(String str) {
if (str == null) return null;
if(str.contains("_")) return str.toLowerCase();
StringBuilder sb = new StringBuilder();
boolean prevUpper = false, curUpper = false, nextUpper = false;
for (int i = 0; i < str.length(); i++) {
char s = str.charAt(i);
prevUpper = curUpper;
curUpper = (i == 0) ? Character.isUpperCase(s) : nextUpper;
nextUpper = (i < str.length() - 1 ? Character.isUpperCase(str.charAt(i + 1)) : true);
if(String.valueOf(s).equals(Symbol.LineUnder)) continue;
if(i > 0 && curUpper && !(nextUpper && prevUpper)) sb.append(Symbol.LineUnder);
sb.append(Character.toLowerCase(s));
}
return sb.toString();
}
/*
* Camel-Case
* user_id - userId
* @param str
* @return Camel-Case
*/
public static String toCamel(String str) {
if (str == null) return null;
if(str.contains(Symbol.LineUnder)) str = str.toLowerCase();
StringBuilder sb = new StringBuilder(str.length());
boolean upperCase = false;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(String.valueOf(c).equals(Symbol.LineUnder)){
upperCase = true;
}else {
if(upperCase){
sb.append(Character.toUpperCase(c));
upperCase = false;
}else {
sb.append(c);
}
}
}
return StringUtils.uncapitalize(sb.toString());
}
/*
* Pascal Case == Upper Camel Case
* userId - UserId
* @param str
* @return Pascal Case
*/
public static String toPascal(String str) {
if (str == null) return null;
return StringUtils.capitalize(toCamel(str));
}
/*
* For example
* [email protected] - http://mail.163.com
* @param email
* @return Email home page link
*/
public static String toEmail(String email) {
return StringUtils.isEmpty(email) ? null :
email.contains("@gmail") ? "https://accounts.google.com" : "http://mail."+email.split("@")[1];
}
/*
* Verify the email
* @param email
* matches {@link com.webapp.utils.regex.RegexConst#Email }
* @return boolean
*/
public static boolean regexEmail(String email){
return StringUtils.isNotEmpty(email) && email.matches(RegexConst.EMAIL);
}
/*
* Verify the mobile phone number
* @param phone
* matches {@link com.webapp.utils.regex.RegexConst#Mobile}
* @return boolean
*/
public static boolean regexMobile(String phone){
return StringUtils.isNotEmpty(phone) && phone.matches(RegexConst.EMAIL);
}
/*
* Verify the identity card
* @param idcard
* matches {@link com.webapp.utils.regex.RegexConst#Idcard}
* @return boolean
*/
public static boolean regexIdcard(String idcard){
return StringUtils.isNotEmpty(idcard) && idcard.matches(RegexConst.EMAIL);
}
/*
* For example
* [email protected] - exam***@163.com
* @param email
* @return The safety of Email
*/
public static String safedEmail(String email) {
return safedEmail(email, 3);
}
/*
* For example
* [email protected] - exam***@163.com
* @param email
* @param len The length of *
* @return The safety of Email
*/
public static String safedEmail(String email, int len) {
if(StringUtils.isNotEmpty(email))
return email.replaceAll("(.{" + len + "})(?=@)", "***");
return null;
}
/*
* For example
* 13888888888 - 138****8888
* @param phone
* @return The safety of mobile
*/
public static String safedMobile(String mobile) {
if(StringUtils.isNotEmpty(mobile))
return mobile.replaceAll("(?<=\\d{3})(.{4})(?=\\d{4})", "****");
return null;
}
/*
* For example
* ["a", "b", "c"] - a,b,c
* @param list
* @return string
*/
public static String split(List list) {
return split(list, Symbol.Comma);
}
/*
* For example
* ["a", "b", "c"] - a,b,c
* @param list
* @param split The separator default ,
* @return string
*/
public static String split(List list, String split) {
return list.stream().map(x->x.toString()).collect(Collectors.joining(split));
}
/*
* For example
* a,b,c, - a,b,c
* @param str
* @return
*/
public static String delTail(String str){
return delTail(str, Symbol.Comma);
}
/*
* For example
* a,b,c, - a,b,c
* @param str
* @param remove Remove the character By default ,
* @return
*/
public static String delTail(String str, String remove){
if(StringUtils.isNotEmpty(str))
return StringUtils.removeEnd(str, remove);
return "";
}
/*
* Is Chinese
* @param str
* @return boolean
*/
public static boolean isChinese(String str){
return isChinese(str, false);
}
/*
* Is Chinese
* @param str
* @return boolean
*/
public static boolean isChinese(String str, boolean hasSymbols) {
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c, hasSymbols)) {
return true;
}
}
return false;
}
private static boolean isChinese(char c, boolean hasSymbols) {
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) {
return true;
}
if (hasSymbols) {
if (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
}
return false;
}
public static String ascii2native(String ascii) {
List ascii_s = new ArrayList();
String regex = "\\\\u[0-9,a-f,A-F]{4}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(ascii);
while (m.find()) {
ascii_s.add(m.group());
}
for (int i = 0, j = 2; i < ascii_s.size(); i++) {
String code = ascii_s.get(i).substring(j, j + 4);
char ch = (char) Integer.parseInt(code, 16);
ascii = ascii.replace(ascii_s.get(i), String.valueOf(ch));
}
return ascii;
}
public static String str2Hex(String str) throws UnsupportedEncodingException {
String hexRaw = String.format("%x", new BigInteger(1, str.getBytes("UTF-8")));
char[] hexRawArr = hexRaw.toCharArray();
StringBuilder hexFmtStr = new StringBuilder();
final String SEP = "\\x";
for (int i = 0; i < hexRawArr.length; i++) {
hexFmtStr.append(SEP).append(hexRawArr[i]).append(hexRawArr[++i]);
}
return hexFmtStr.toString();
}
public static String hex2Str(String str) throws UnsupportedEncodingException {
String strArr[] = str.split("\\\\");
byte[] byteArr = new byte[strArr.length - 1];
for (int i = 1; i < strArr.length; i++) {
Integer hexInt = Integer.decode("0" + strArr[i]);
byteArr[i - 1] = hexInt.byteValue();
}
return new String(byteArr, "UTF-8");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy