com.feingto.cloud.kit.StringKit Maven / Gradle / Ivy
package com.feingto.cloud.kit;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 字符串转换工具
*
* @author longfei
*/
@Slf4j
@SuppressWarnings("unused")
public class StringKit {
/**
* 逗号分割字符串
*
* @param str String 原始字符串
* @return String[] 分割后的字符串数组
*/
public static String[] split(String str) {
return split(str, ",");
}
/**
* 分割字符串
*
* @param str String 原始字符串
* @param splitsign String 分隔符
* @return String[] 分割后的字符串数组
*/
public static String[] split(String str, String splitsign) {
int index;
if (str == null || splitsign == null) {
return null;
}
ArrayList al = new ArrayList<>();
while ((index = str.indexOf(splitsign)) != -1) {
al.add(str.substring(0, index));
str = str.substring(index + splitsign.length());
}
al.add(str);
return al.toArray(new String[0]);
}
/**
* 替换字符串
*
* @param from String 原始字符串
* @param to String 目标字符串
* @param source String 母字符串
* @return String 替换后的字符串
*/
public static String replace(String from, String to, String source) {
if (source == null || from == null || to == null)
return null;
StringBuilder str = new StringBuilder("");
int index;
while ((index = source.indexOf(from)) != -1) {
str.append(source.substring(0, index)).append(to);
source = source.substring(index + from.length());
}
str.append(source);
return str.toString();
}
/**
* 替换字符串,能能够在HTML页面上直接显示(替换双引号和小于号)
*
* @param str String 原始字符串
* @return String 替换后的字符串
*/
public static String htmlencode(String str) {
return str == null ? null : replace("\"", """, replace("<", "<", str));
}
/**
* 替换字符串,将被编码的转换成原始码(替换成双引号和小于号)
*
* @param str String
* @return String
*/
public static String htmldecode(String str) {
return str == null ? null : replace(""", "\"", replace("<", "<", str));
}
/**
* 在页面上直接显示文本内容,替换小于号,空格,回车,TAB
*
* @param str String 原始字符串
* @return String 替换后的字符串
*/
public static String htmlshow(String str) {
if (str == null) {
return null;
} else {
str = replace("<", "<", str);
str = replace(" ", " ", str);
str = replace("\r\n", "
", str);
str = replace("\n", "
", str);
str = replace("\t", " ", str);
return str;
}
}
public static String htmlToText(String str) {
String regEx_script = "