PublicAffairs/gather-app/src/main/java/com/ruoyi/business/util/StringKit.java

123 lines
3.3 KiB
Java
Raw Normal View History

package com.ruoyi.business.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringKit {
public StringKit() {
}
public static Boolean isEmpty(Object argObject) {
Boolean tbool = false;
String tString = toString(argObject);
if (tString.equals("")) {
tbool = true;
}
return tbool;
}
public static String toString(Object argObject) {
String tmpstr = "";
try {
if (argObject != null) {
tmpstr = argObject.toString();
if ("null".equals(tmpstr) || "underfind".equals(tmpstr) || "undefined".equals(tmpstr) || "{}".equals(tmpstr)) {
tmpstr = "";
}
}
} catch (Exception var3) {
}
return tmpstr;
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
public static boolean isEng(String str) {
Pattern pattern = Pattern.compile("[a-zA-Z]");
return pattern.matcher(str).matches();
}
public static String getTrace(Throwable t) {
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
t.printStackTrace(writer);
StringBuffer buffer = stringWriter.getBuffer();
return buffer.toString();
}
/**
* 空时 返回NA(有的厂家字段必填我们没有先用NA补充)
*
* @return
*/
public static String responseNa(Object object) {
if (isEmpty(object)) {
return "NA";
}
return String.valueOf(object);
}
/**
* 检查某个字符串出现几次
*
* @return
*/
public static Integer getStrCount(String str, String indexStr) {
//java中判断一个字符出现的次数
//在下面字符串中查找有几个啊
// String str = "啊!我爱你中国!啊,我爱你故乡";
//存放每个字符的数组
String[] strs = new String[str.length()];
//计数该字符出现了多少次
int count = 0;
//先把字符串转换成数组
for (int i = 0; i < strs.length; i++) {
strs[i] = str.substring(i, i + 1);
}
//挨个字符进行查找查找到之后count++
for (int i = 0; i < strs.length; i++) {
if (strs[i].equals(indexStr)) {
count++;
}
}
// System.out.println("一共有"+count+"个啊");
return count;
}
public static Map<String, String> parse(String input) {
Map<String, String> result = new HashMap<>();
// 定义正则表达式
String regex = "(\\w+)=\"([^\"]*)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
// 使用正则表达式进行匹配
while (matcher.find()) {
// 提取键值对
String key = matcher.group(1);
String value = matcher.group(2);
result.put(key, value);
}
return result;
}
}