io.github.kits.StringKit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimthen-kits Show documentation
Show all versions of whimthen-kits Show documentation
Easy to use java tool library.
The newest version!
package io.github.kits;
import io.github.kits.cache.PatternCache;
import io.github.kits.json.tokenizer.CharReader;
import io.github.kits.log.Logger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
/**
* 字符串操作工具类
*
* @project: kits
* @created: with IDEA
* @author: kits
* @date: 2018 04 26 下午5:58 | 四月. 星期四
*/
public class StringKit {
/**
* 常用到的字符
*/
public static String[] CHARS = new String[]{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
/**
* 获取所有字符的字符串
*
* @return
*/
public static String getChars() {
return String.join("", CHARS);
}
/**
* 判断是否为空
*
* @param value 需要判断的字符串
* @return true-null or empty | false-not null Or empty
*/
public static boolean isNullOrEmpty(String... value) {
if (null == value || value.length == 0) {
return true;
}
for (String val : value) {
if (null == val || val.isEmpty()) {
return true;
}
}
return false;
}
/**
* 判断是否不为空
*
* @param value 需要判断的字符串
* @return true-not null or empty | false-null or empty
*/
public static boolean isNotNullOrEmpty(String... value) {
return !isNullOrEmpty(value);
}
/**
* 将多个正则匹配到的内容替换为相同的内容
*
* @param resource 资源内容
* @param replacement 替换的目标内容
* @param regex 正则
* @return 替换后的字符串
*/
public static String replaceAll(String resource, String replacement, String... regex) {
if (isNullOrEmpty(resource) || regex.length == 0) {
return resource;
}
String result = resource;
for (String reg : regex) {
result = PatternCache.get(reg)
.matcher(result)
.replaceAll(replacement == null ? "" : replacement);
}
return result;
}
/**
* 缩进字符串
*
* @param source 待缩进的字符串
* @param indentCount 缩进数(空格的数目)
* @return 缩进后的字符串
*/
public static String indent(String source, int indentCount) {
if (indentCount > 0 && source != null) {
StringBuilder indent = new StringBuilder();
indent.append(repeat(" ", indentCount));
source = indent.toString() + source;
source = replaceAll(source, "\n" + indent.toString(), "\n");
}
return source;
}
public static String radixConvert(long num, int radix) {
if (radix < 2 || radix > 62) {
return null;
}
num = num < 0 ? num * -1 : num;
StringBuilder result = new StringBuilder();
long tmpValue = num;
while (true) {
long value = (int) (tmpValue % radix);
result.insert(0, CHARS[(int) value]);
value = tmpValue / radix;
if (value >= radix) {
tmpValue = value;
} else {
result.insert(0, CHARS[(int) value]);
break;
}
}
return result.toString();
}
/**
* 复制字符串
*
* @param resource 需要重复的字符串资源
* @param count 重复之后共次数
* @return 重复之后的字符串
*/
public static String repeat(String resource, int count) {
if (Objects.isNull(resource)) {
throw new IllegalArgumentException("resource is null!");
}
if (count < 0) {
throw new IllegalArgumentException("count is negative: " + count);
}
int len = resource.length();
if (len == 0 || count == 0) {
return "";
}
if (count == 1) {
return resource;
}
if (Integer.MAX_VALUE / count < len) {
throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
" times will produce a String exceeding maximum size.");
}
StringBuilder repeatString = new StringBuilder(resource);
for (int i = 1; i < count; i++) {
repeatString.append(resource);
}
return repeatString.toString();
}
/**
* 获取字符在字符串中出现的次数
*
* @param resource 源
* @param val 需要计数的字符
* @return 次数
*/
public static int getCharCount(String resource, char val) {
int count = 0;
if (isNotNullOrEmpty(resource)) {
for (char ch : resource.toCharArray()) {
if (ch == val) {
count++;
}
}
}
return count;
}
public static List toCharList(String val) {
List resp = null;
if (isNotNullOrEmpty(val)) {
resp = new ArrayList<>();
for (Character v : val.toCharArray()) {
resp.add(v.toString());
}
}
return resp;
}
/**
* 判断正则是否匹配
*
* @param resource
* @param regex
* @return
*/
public static boolean regexMatch(String resource, String regex) {
return PatternCache.get(regex)
.matcher(resource)
.matches();
}
/**
* 判断正则是否匹配
*
* @param resource
* @param regex
* @return
*/
public static boolean regexFind(String resource, String regex) {
return PatternCache.get(regex)
.matcher(resource)
.find();
}
/**
* 判断正则是否至少有一个匹配
*
* @param resource
* @param regexs
* @return
*/
public static boolean regexFindByMultiRegex(String resource, List regexs) {
Optional any = regexs.parallelStream()
.filter(regex -> PatternCache.get(regex)
.matcher(resource)
.find())
.findAny();
return any.isPresent();
}
public static int searchByRegex(String source, String regex) {
if (source == null) {
return 0;
}
ArrayList result = new ArrayList();
Matcher matcher = PatternCache.get(regex)
.matcher(source);
if (matcher.find()) {
do {
result.add(matcher.group());
} while (matcher.find());
}
return result.size();
}
/**
* 判断字符串是否为数字类型
* 整数型、浮点型
*
* @param val 需要判断的值
* @return true-是数字类型 | false-不是数字类型
*/
public static boolean isNumber(String val, boolean isFloating) {
if (isNullOrEmpty(val)) {
return false;
}
if (!isFloating && val.contains(".")) {
return false;
}
char[] chars = val.toCharArray();
if (chars[0] == '.' || chars[chars.length - 1] == '.'
|| chars[0] == 'e' || chars[0] == 'E') {
throw new IllegalArgumentException("The value first or last char can't be [., e, E]");
}
for (char ch : chars) {
if (ch != '.' && ch != 'e' && ch != 'E' && (ch < '0' || ch > '9')) {
return false;
}
}
return true;
}
public static boolean isNumber(String val) {
return isNumber(val, false);
}
/**
* 首字母大写
*
* @param val 需要设置的值
* @return 设置后的值
*/
public static String toUpper1(String val) {
if (isNullOrEmpty(val)) {
return null;
}
char ch = val.charAt(0);
if (ch >= 'a' && ch <= 'z') {
return ((char) (ch - 32)) + val.substring(1);
} else {
return val;
}
}
/**
* CameCase is converted to underline form
*
* @param val CameCase String
* @return underline String
* @throws IOException Read String Exception
*/
public static String unCameCase(String val) throws IOException {
if (isNotNullOrEmpty(val)) {
CharReader charReader = new CharReader(val);
StringBuilder result = new StringBuilder();
while (charReader.hasMore()) {
char ch = charReader.next();
if (ch >= 'A' && ch <= 'Z') {
result.append('_')
.append((char) (ch + 32));
} else if (ch != '_') {
result.append(ch);
}
}
return result.toString();
}
return "";
}
/**
* Underline is convertd to CameCase form
*
* @param val Underline String
* @return CameCase String
* @throws IOException Read Exception
*/
public static String cameCase(String val) throws IOException {
if (isNotNullOrEmpty(val)) {
CharReader charReader = new CharReader(val);
StringBuilder result = new StringBuilder(String.valueOf(charReader.next()));
while (charReader.hasMore()) {
char ch = charReader.next();
if (ch == '_') {
char next = charReader.next();
if (next != '_') {
result.append((char) (next - 32));
}
} else {
result.append(ch);
}
}
return result.toString();
}
return "";
}
}