
cn.gnux.core.utils.StrUtil Maven / Gradle / Ivy
The newest version!
package cn.gnux.core.utils;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串工具类
*
*
*/
public class StrUtil {
public static final String SPACE = " ";
public static final String DOT = ".";
public static final String SLASH = "/";
public static final String BACKSLASH = "\\";
public static final String EMPTY = "";
public static final String CRLF = "\r\n";
public static final String NEWLINE = "\n";
public static final String UNDERLINE = "_";
public static final String COMMA = ",";
public static final String HTML_NBSP = " ";
public static final String HTML_AMP = "&";
public static final String HTML_QUOTE = """;
public static final String HTML_LT = "<";
public static final String HTML_GT = ">";
public static final String EMPTY_JSON = "{}";
/**
* 字符串是否为空白 空白的定义如下:
* 1、为null
* 2、为不可见字符(如空格)
* 3、""
*
* @param str 被检测的字符串
* @return 是否为空
*/
public static boolean isBlank(String str) {
return str == null || str.trim().length() == 0;
}
/**
* 字符串是否为非空白 空白的定义如下:
* 1、不为null
* 2、不为不可见字符(如空格)
* 3、不为""
*
* @param str 被检测的字符串
* @return 是否为非空
*/
public static boolean isNotBlank(String str) {
return false == isBlank(str);
}
/**
* 是否包含空字符串
*
* @param strs 字符串列表
* @return 是否包含空字符串
*/
public static boolean hasBlank(String... strs) {
if (CollectionUtil.isEmpty(strs)) {
return true;
}
for (String str : strs) {
if (isBlank(str)) {
return true;
}
}
return false;
}
/**
* 字符串是否为空,空的定义如下 1、为null
* 2、为""
*
* @param str 被检测的字符串
* @return 是否为空
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
/**
* 字符串是否为非空白 空白的定义如下:
* 1、不为null
* 2、不为""
*
* @param str 被检测的字符串
* @return 是否为非空
*/
public static boolean isNotEmpty(String str) {
return false == isEmpty(str);
}
/**
* 当给定字符串为null时,转换为Empty
*
* @param str 被转换的字符串
* @return 转换后的字符串
*/
public static String nullToEmpty(String str) {
return nullToDefault(str, EMPTY);
}
/**
* 如果字符串是null
,则返回指定默认字符串,否则返回字符串本身。
*
*
* nullToDefault(null, "default") = "default"
* nullToDefault("", "default") = ""
* nullToDefault(" ", "default") = " "
* nullToDefault("bat", "default") = "bat"
*
*
* @param str 要转换的字符串
* @param defaultStr 默认字符串
*
* @return 字符串本身或指定的默认字符串
*/
public static String nullToDefault(String str, String defaultStr) {
return (str == null) ? defaultStr : str;
}
/**
* 当给定字符串为空字符串时,转换为null
*
* @param str 被转换的字符串
* @return 转换后的字符串
*/
public static String emptyToNull(String str) {
return isEmpty(str) ? null : str;
}
/**
* 是否包含空字符串
*
* @param strs 字符串列表
* @return 是否包含空字符串
*/
public static boolean hasEmpty(String... strs) {
for (String str : strs) {
if (isEmpty(str)) {
return true;
}
}
return false;
}
/**
* 去除字符串两边的空格符,如果为null返回null
*
* @param str 字符串
* @return 处理后的字符串
*/
public static String trim(String str) {
return (null == str) ? null : str.trim();
}
/**
* 获得set或get方法对应的标准属性名
* 例如:setName 返回 name
*
* @param getOrSetMethodName
* @return 如果是set或get方法名,返回field, 否则null
*/
public static String getGeneralField(String getOrSetMethodName) {
if (getOrSetMethodName.startsWith("get") || getOrSetMethodName.startsWith("set")) {
return cutPreAndLowerFirst(getOrSetMethodName, 3);
}
return null;
}
/**
* 生成set方法名
* 例如:name 返回 setName
*
* @param fieldName 属性名
* @return setXxx
*/
public static String genSetter(String fieldName) {
return upperFirstAndAddPre(fieldName, "set");
}
/**
* 生成get方法名
*
* @param fieldName 属性名
* @return getXxx
*/
public static String genGetter(String fieldName) {
return upperFirstAndAddPre(fieldName, "get");
}
/**
* 去掉首部指定长度的字符串并将剩余字符串首字母小写
* 例如:str=setName, preLength=3 -> return name
*
* @param str 被处理的字符串
* @param preLength 去掉的长度
* @return 处理后的字符串,不符合规范返回null
*/
public static String cutPreAndLowerFirst(String str, int preLength) {
if (str == null) {
return null;
}
if (str.length() > preLength) {
char first = Character.toLowerCase(str.charAt(preLength));
if (str.length() > preLength + 1) {
return first + str.substring(preLength + 1);
}
return String.valueOf(first);
}
return null;
}
/**
* 原字符串首字母大写并在其首部添加指定字符串 例如:str=name, preString=get -> return getName
*
* @param str 被处理的字符串
* @param preString 添加的首部
* @return 处理后的字符串
*/
public static String upperFirstAndAddPre(String str, String preString) {
if (str == null || preString == null) {
return null;
}
return preString + upperFirst(str);
}
/**
* 大写首字母
* 例如:str = name, return Name
*
* @param str 字符串
* @return 字符串
*/
public static String upperFirst(String str) {
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
/**
* 将字符串首字母大写后返回
*
* @param str 原字符串
* @return 首字母大写后的字符串
*
*
* capitalizeFirstLetter(null) = null;
* capitalizeFirstLetter("") = "";
* capitalizeFirstLetter("2ab") = "2ab"
* capitalizeFirstLetter("a") = "A"
* capitalizeFirstLetter("ab") = "Ab"
* capitalizeFirstLetter("Abc") = "Abc"
*
*/
public static String capitalizeFirstLetter(String str) {
if (isEmpty(str)) {
return str;
}
char c = str.charAt(0);
return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : new StringBuilder(str.length()).append(Character.toUpperCase(c)).append(str.substring(1)).toString();
}
/**
* 小写首字母
* 例如:str = Name, return name
*
* @param str 字符串
* @return 字符串
*/
public static String lowerFirst(String str) {
return Character.toLowerCase(str.charAt(0)) + str.substring(1);
}
/**
* 去掉指定前缀
*
* @param str 字符串
* @param prefix 前缀
* @return 切掉后的字符串,若前缀不是 preffix, 返回原字符串
*/
public static String removePrefix(String str, String prefix) {
if (str != null && str.startsWith(prefix)) {
return str.substring(prefix.length());
}
return str;
}
/**
* 忽略大小写去掉指定前缀
*
* @param str 字符串
* @param prefix 前缀
* @return 切掉后的字符串,若前缀不是 prefix, 返回原字符串
*/
public static String removePrefixIgnoreCase(String str, String prefix) {
if (str != null && str.toLowerCase().startsWith(prefix.toLowerCase())) {
return str.substring(prefix.length());
}
return str;
}
/**
* 去掉指定后缀
*
* @param str 字符串
* @param suffix 后缀
* @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串
*/
public static String removeSuffix(String str, String suffix) {
if (str != null && str.endsWith(suffix)) {
return str.substring(0, str.length() - suffix.length());
}
return str;
}
/**
* 忽略大小写去掉指定后缀
*
* @param str 字符串
* @param suffix 后缀
* @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串
*/
public static String removeSuffixIgnoreCase(String str, String suffix) {
if (str != null && str.toLowerCase().endsWith(suffix.toLowerCase())) {
return str.substring(0, str.length() - suffix.length());
}
return str;
}
/**
* 清理空白字符
*
* @param str 被清理的字符串
* @return 清理后的字符串
*/
public static String cleanBlank(String str) {
if (str == null) {
return null;
}
return str.replaceAll("\\s*", EMPTY);
}
/**
* 切分字符串
* a#b#c -> [a,b,c]
* a##b#c -> [a,"",b,c]
*
* @param str 被切分的字符串
* @param separator 分隔符字符
* @return 切分后的集合
*/
public static List split(String str, char separator) {
return split(str, separator, 0);
}
/**
* 切分字符串
*
* @param str 被切分的字符串
* @param separator 分隔符字符
* @param limit 限制分片数
* @return 切分后的集合
*/
public static List split(String str, char separator, int limit) {
if (str == null) {
return null;
}
List list = new ArrayList(limit == 0 ? 16 : limit);
if (limit == 1) {
list.add(str);
return list;
}
boolean isNotEnd = true; // 未结束切分的标志
int strLen = str.length();
StringBuilder sb = new StringBuilder(strLen);
for (int i = 0; i < strLen; i++) {
char c = str.charAt(i);
if (isNotEnd && c == separator) {
list.add(sb.toString());
// 清空StringBuilder
sb.delete(0, sb.length());
// 当达到切分上限-1的量时,将所剩字符全部作为最后一个串
if (limit != 0 && list.size() == limit - 1) {
isNotEnd = false;
}
} else {
sb.append(c);
}
}
list.add(sb.toString());// 加入尾串
return list;
}
/**
* 切分字符串
* from jodd
*
* @param str 被切分的字符串
* @param delimiter 分隔符
* @return 字符串
*/
public static String[] split(String str, String delimiter) {
if (str == null) {
return null;
}
if (str.trim().length() == 0) {
return new String[] { str };
}
int dellen = delimiter.length(); // del length
int maxparts = (str.length() / dellen) + 2; // one more for the last
int[] positions = new int[maxparts];
int i, j = 0;
int count = 0;
positions[0] = -dellen;
while ((i = str.indexOf(delimiter, j)) != -1) {
count++;
positions[count] = i;
j = i + dellen;
}
count++;
positions[count] = str.length();
String[] result = new String[count];
for (i = 0; i < count; i++) {
result[i] = str.substring(positions[i] + dellen, positions[i + 1]);
}
return result;
}
/**
* 改进JDK subString
* index从0开始计算,最后一个字符为-1
* 如果from和to位置一样,返回 "" example: abcdefgh 2 3 -> c abcdefgh 2 -3 -> cde
*
* @param string String
* @param fromIndex 开始的index(包括)
* @param toIndex 结束的index(不包括)
* @return 字串
*/
public static String sub(String string, int fromIndex, int toIndex) {
int len = string.length();
if (fromIndex < 0) {
fromIndex = len + fromIndex;
if (toIndex == 0) {
toIndex = len;
}
}
if (toIndex < 0) {
toIndex = len + toIndex;
}
if (toIndex < fromIndex) {
int tmp = fromIndex;
fromIndex = toIndex;
toIndex = tmp;
}
if (fromIndex == toIndex) {
return EMPTY;
}
char[] strArray = string.toCharArray();
char[] newStrArray = Arrays.copyOfRange(strArray, fromIndex, toIndex);
return new String(newStrArray);
}
/**
* 切割前部分
*
* @param string 字符串
* @param toIndex 切割到的位置(不包括)
* @return 切割后的字符串
*/
public static String subPre(String string, int toIndex) {
return sub(string, 0, toIndex);
}
/**
* 切割后部分
*
* @param string 字符串
* @param fromIndex 切割开始的位置(包括)
* @return 切割后的字符串
*/
public static String subSuf(String string, int fromIndex) {
if (isEmpty(string)) {
return null;
}
return sub(string, fromIndex, string.length());
}
/**
* 给定字符串是否被字符包围
*
* @param str 字符串
* @param prefix 前缀
* @param suffix 后缀
* @return 是否包围,空串不包围
*/
public static boolean isSurround(String str, String prefix, String suffix) {
if (StrUtil.isBlank(str)) {
return false;
}
if (str.length() < (prefix.length() + suffix.length())) {
return false;
}
return str.startsWith(prefix) && str.endsWith(suffix);
}
/**
* 给定字符串是否被字符包围
*
* @param str 字符串
* @param prefix 前缀
* @param suffix 后缀
* @return 是否包围,空串不包围
*/
public static boolean isSurround(String str, char prefix, char suffix) {
if (StrUtil.isBlank(str)) {
return false;
}
if (str.length() < 2) {
return false;
}
return str.charAt(0) == prefix && str.charAt(str.length() - 1) == suffix;
}
/**
* 重复某个字符
*
* @param c 被重复的字符
* @param count 重复的数目
* @return 重复字符字符串
*/
public static String repeat(char c, int count) {
char[] result = new char[count];
for (int i = 0; i < count; i++) {
result[i] = c;
}
return new String(result);
}
/**
* 重复某个字符串
*
* @param str 被重复的字符
* @param count 重复的数目
* @return 重复字符字符串
*/
public static String repeat(String str, int count) {
// 检查
final int len = str.length();
final long longSize = (long) len * (long) count;
final int size = (int) longSize;
if (size != longSize) {
throw new ArrayIndexOutOfBoundsException("Required String length is too large: " + longSize);
}
final char[] array = new char[size];
str.getChars(0, len, array, 0);
int n;
for (n = len; n < size - n; n <<= 1) {// n <<= 1相当于n *2
System.arraycopy(array, 0, array, n, n);
}
System.arraycopy(array, 0, array, n, size - n);
return new String(array);
}
/**
* 比较两个字符串是否相同,如果为null或者空串则算不同
*
* @param str1 字符串1
* @param str2 字符串2
* @return 是否非空相同
*/
public static boolean equalsNotEmpty(String str1, String str2) {
if (isEmpty(str1)) {
return false;
}
return str1.equals(str2);
}
/**
* 格式化文本
*
* @param template 文本模板,被替换的部分用 {} 表示
* @param values 参数值
* @return 格式化后的文本
*/
public static String format(String template, Object... values) {
if (CollectionUtil.isEmpty(values) || isBlank(template)) {
return template;
}
final StringBuilder sb = new StringBuilder();
final int length = template.length();
int valueIndex = 0;
char currentChar;
for (int i = 0; i < length; i++) {
if (valueIndex >= values.length) {
sb.append(sub(template, i, length));
break;
}
currentChar = template.charAt(i);
if (currentChar == '{') {
final char nextChar = template.charAt(++i);
if (nextChar == '}') {
sb.append(values[valueIndex++]);
} else {
sb.append('{').append(nextChar);
}
} else {
sb.append(currentChar);
}
}
return sb.toString();
}
/**
* 格式化文本
*
* @param template 文本模板,被替换的部分用 {key} 表示
* @param map 参数值对
* @return 格式化后的文本
*/
public static String format(String template, Map, ?> map) {
if (null == map || map.isEmpty()) {
return template;
}
for (Entry, ?> entry : map.entrySet()) {
template = template.replace("{" + entry.getKey() + "}", entry.getValue().toString());
}
return template;
}
/**
* 编码字符串
*
* @param str 字符串
* @param charset 字符集,如果此字段为空,则解码的结果取决于平台
* @return 编码后的字节码
*/
public static byte[] encode(String str, String charset) {
if (str == null) {
return null;
}
if (isBlank(charset)) {
return str.getBytes();
}
try {
return str.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(format("Charset [{}] unsupported!", charset));
}
}
/**
* 解码字节码
*
* @param data 字符串
* @param charset 字符集,如果此字段为空,则解码的结果取决于平台
* @return 解码后的字符串
*/
public static String decode(byte[] data, String charset) {
if (data == null) {
return null;
}
if (isBlank(charset)) {
return new String(data);
}
try {
return new String(data, charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(format("Charset [{}] unsupported!", charset));
}
}
/**
* 将多个对象字符化
* 每个对象字符化后直接拼接,无分隔符
*
* @param objs 对象数组
* @return 字符串
*/
public static String str(Object... objs) {
StringBuilder sb = new StringBuilder();
for (Object obj : objs) {
sb.append(obj);
}
return sb.toString();
}
/**
* 将byte数组转为字符串
*
* @param bytes byte数组
* @param charset 字符集
* @return 字符串
*/
public static String str(byte[] bytes, String charset) {
return new String(bytes, Charset.forName(charset));
}
/**
* 将驼峰式命名的字符串转换为下划线方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。
* 例如:HelloWorld->hello_world
*
* @param camelCaseStr 转换前的驼峰式命名的字符串
* @return 转换后下划线大写方式命名的字符串
*/
public static String toUnderlineCase(String camelCaseStr) {
if (camelCaseStr == null) {
return null;
}
final int length = camelCaseStr.length();
StringBuilder sb = new StringBuilder();
char c;
boolean isPreUpperCase = false;
for (int i = 0; i < length; i++) {
c = camelCaseStr.charAt(i);
boolean isNextUpperCase = true;
if (i < (length - 1)) {
isNextUpperCase = Character.isUpperCase(camelCaseStr.charAt(i + 1));
}
if (Character.isUpperCase(c)) {
if (!isPreUpperCase || !isNextUpperCase) {
if (i > 0) sb.append(UNDERLINE);
}
isPreUpperCase = true;
} else {
isPreUpperCase = false;
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
/**
* 将下划线方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
* 例如:hello_world->HelloWorld
*
* @param name 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
*/
public static String toCamelCase(String name) {
if (name == null) {
return null;
}
if (name.contains(UNDERLINE)) {
name = name.toLowerCase();
StringBuilder sb = new StringBuilder(name.length());
boolean upperCase = false;
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c == '_') {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
} else
return name;
}
/**
* 包装指定字符串
*
* @param str 被包装的字符串
* @param prefix 前缀
* @param suffix 后缀
* @return 包装后的字符串
*/
public static String wrap(String str, String prefix, String suffix) {
return format("{}{}{}", prefix, str, suffix);
}
/**
* 指定字符串是否被包装
*
* @param str 字符串
* @param prefix 前缀
* @param suffix 后缀
* @return 是否被包装
*/
public static boolean isWrap(String str, String prefix, String suffix) {
return str.startsWith(prefix) && str.endsWith(suffix);
}
/**
* 指定字符串是否被同一字符包装(前后都有这些字符串)
*
* @param str 字符串
* @param wrapper 包装字符串
* @return 是否被包装
*/
public static boolean isWrap(String str, String wrapper) {
return isWrap(str, wrapper, wrapper);
}
/**
* 指定字符串是否被同一字符包装(前后都有这些字符串)
*
* @param str 字符串
* @param wrapper 包装字符
* @return 是否被包装
*/
public static boolean isWrap(String str, char wrapper) {
return isWrap(str, wrapper, wrapper);
}
/**
* 指定字符串是否被包装
*
* @param str 字符串
* @param prefixChar 前缀
* @param suffixChar 后缀
* @return 是否被包装
*/
public static boolean isWrap(String str, char prefixChar, char suffixChar) {
return str.charAt(0) == prefixChar && str.charAt(str.length() - 1) == suffixChar;
}
/**
* 补充字符串以满足最小长度 StrUtil.padPre("1", 3, '0');//"001"
*
* @param str 字符串
* @param minLength 最小长度
* @param padChar 补充的字符
* @return 补充后的字符串
*/
public static String padPre(String str, int minLength, char padChar) {
if (str.length() >= minLength) {
return str;
}
StringBuilder sb = new StringBuilder(minLength);
for (int i = str.length(); i < minLength; i++) {
sb.append(padChar);
}
sb.append(str);
return sb.toString();
}
/**
* 补充字符串以满足最小长度 StrUtil.padEnd("1", 3, '0');//"100"
*
* @param str 字符串
* @param minLength 最小长度
* @param padChar 补充的字符
* @return 补充后的字符串
*/
public static String padEnd(String str, int minLength, char padChar) {
if (str.length() >= minLength) {
return str;
}
StringBuilder sb = new StringBuilder(minLength);
sb.append(str);
for (int i = str.length(); i < minLength; i++) {
sb.append(padChar);
}
return sb.toString();
}
/**
* 创建StringBuilder对象
*
* @return StringBuilder对象
*/
public static StringBuilder builder() {
return new StringBuilder();
}
/**
* 创建StringBuilder对象
*
* @return StringBuilder对象
*/
public static StringBuilder builder(int capacity) {
return new StringBuilder(capacity);
}
/**
* 创建StringBuilder对象
*
* @return StringBuilder对象
*/
public static StringBuilder builder(String... strs) {
final StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.append(str);
}
return sb;
}
/**
* 获得字符串对应字符集的byte数组
* 调用encode方法
*
* @param str 字符串
* @param charset 字符集编码
* @return byte数组
*/
public static byte[] bytes(String str, String charset) {
return encode(str, charset);
}
/**
* 获得StringReader
*
* @param str 字符串
* @return StringReader
*/
public static StringReader getReader(String str) {
return new StringReader(str);
}
/**
* 获得StringWriter
*
* @return StringWriter
*/
public static StringWriter getWriter() {
return new StringWriter();
}
/**
* 如果不是普通字符,则按照utf8进行编码
*
*
* utf8Encode(null) = null
* utf8Encode("") = "";
* utf8Encode("aa") = "aa";
* utf8Encode("啊啊啊啊") = "%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A";
*
*
* @param str 原字符
* @return 编码后字符,若编码异常抛出异常
*/
public static String utf8Encode(String str) {
if(!isEmpty(str) && str.getBytes().length != str.length()) {
try {
return URLEncoder.encode(str,"UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UnsupportedEncodingException occurred. ", e);
}
}
return str;
}
/**
* 如果不是普通字符,则按照utf8进行编码,编码异常则返回defultReturn
*
* @param str 源字符串
* @param defultReturn 出现异常默认返回
* @return
*/
public static String utf8Encode(String str, String defaultReturn) {
if (!isEmpty(str) && str.getBytes().length != str.length()) {
try {
return URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
return defaultReturn;
}
}
return str;
}
/**
* 得到href链接的innerHtml
*
* @param href href内容
* @return href的innerHtml
*
* - 空字符串返回""
* - 若字符串不为空,且不符合链接正则的返回原内容
* - 若字符串不为空,且符合链接正则的返回最后一个innerHtml
*
* @see
*
* getHrefInnerHtml(null) = ""
* getHrefInnerHtml("") = ""
* getHrefInnerHtml("mp3") = "mp3";
* getHrefInnerHtml("<a innerHtml</a>") = "<a innerHtml</a>";
* getHrefInnerHtml("<a>innerHtml</a>") = "innerHtml";
* getHrefInnerHtml("<a<a>innerHtml</a>") = "innerHtml";
* getHrefInnerHtml("<a href="baidu.com">innerHtml</a>") = "innerHtml";
* getHrefInnerHtml("<a href="baidu.com" title="baidu">innerHtml</a>") = "innerHtml";
* getHrefInnerHtml(" <a>innerHtml</a> ") = "innerHtml";
* getHrefInnerHtml("<a>innerHtml</a></a>") = "innerHtml";
* getHrefInnerHtml("jack<a>innerHtml</a></a>") = "innerHtml";
* getHrefInnerHtml("<a>innerHtml1</a><a>innerHtml2</a>") = "innerHtml2";
*
*/
public static String getHrefInnerHtml(String href) {
if (isEmpty(href)) {
return "";
}
String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);
Matcher hrefMatcher = hrefPattern.matcher(href);
if (hrefMatcher.matches()) {
return hrefMatcher.group(1);
}
return href;
}
/**
* html的转义字符转换成正常的字符串
*
*
* htmlEscapeCharsToString(null) = null;
* htmlEscapeCharsToString("") = "";
* htmlEscapeCharsToString("mp3") = "mp3";
* htmlEscapeCharsToString("mp3<") = "mp3<";
* htmlEscapeCharsToString("mp3>") = "mp3\>";
* htmlEscapeCharsToString("mp3&mp4") = "mp3&mp4";
* htmlEscapeCharsToString("mp3"mp4") = "mp3\"mp4";
* htmlEscapeCharsToString("mp3<>&"mp4") = "mp3\<\>&\"mp4";
*
*
* @param source
* @return
*/
public static String htmlEscapeCharsToString(String source) {
if (StrUtil.isEmpty(source)) {
return source;
} else {
return source.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&").replaceAll(""",
"\"");
}
}
/**
* 半角字符转换为全角字符
*
*
* fullWidthToHalfWidth(null) = null;
* fullWidthToHalfWidth("") = "";
* fullWidthToHalfWidth(new String(new char[] {12288})) = " ";
* fullWidthToHalfWidth("!"#$%&) = "!\"#$%&";
*
*
* @param s
* @return
*/
public static String fullWidthToHalfWidth(String s) {
if (isEmpty(s)) {
return s;
}
char[] source = s.toCharArray();
for (int i = 0; i < source.length; i++) {
if (source[i] == 12288) {
source[i] = ' ';
} else if (source[i] >= 65281 && source[i] <= 65374) {
source[i] = (char)(source[i] - 65248);
} else {
source[i] = source[i];
}
}
return new String(source);
}
/**
* 全角字符转换为半角字符
*
*
* halfWidthToFullWidth(null) = null;
* halfWidthToFullWidth("") = "";
* halfWidthToFullWidth(" ") = new String(new char[] {12288});
* halfWidthToFullWidth("!\"#$%&) = "!"#$%&";
*
*
* @param s
* @return
*/
public static String halfWidthToFullWidth(String s) {
if (isEmpty(s)) {
return s;
}
char[] source = s.toCharArray();
for (int i = 0; i < source.length; i++) {
if (source[i] == ' ') {
source[i] = (char)12288;
// } else if (source[i] == '.') {
// source[i] = (char)12290;
} else if (source[i] >= 33 && source[i] <= 126) {
source[i] = (char)(source[i] + 65248);
} else {
source[i] = source[i];
}
}
return new String(source);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy