ssjygl-xsct-service/ssjygl-xsx-common/src/main/java/com/cowr/common/utils/StrUtil.java

208 lines
5.7 KiB
Java
Raw Normal View History

2020-08-07 17:11:12 +08:00
package com.cowr.common.utils;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import java.util.Collection;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StrUtil {
public static final String regphone = "^1[3|4|5|7|8|9][\\d]{9}$"; // 放的比较宽
2020-08-12 16:59:27 +08:00
public static final String regphone_tel = "^((0\\d{2,3}-\\d{7,8})|(1[3584]\\d{9}))$"; // 座机或者手机
2020-08-07 17:11:12 +08:00
public static final String DEFAULT_PATH_SEPARATOR = ",";
/**
* adcd
* 129
*
* @param adcd
* @return
*/
public static String getLikeAdcd(String adcd) {
if (StrKit.isBlank(adcd)) {
return "";
}
if ((adcd.length() == 15 && adcd.endsWith("000000000")) || (adcd.length() == 12 && adcd.endsWith("000000"))) {
return adcd.substring(0, 6) + "%";
}
if ((adcd.length() == 15 && adcd.endsWith("000000")) || (adcd.length() == 12 && adcd.endsWith("000"))) {
return adcd.substring(0, 9) + "%";
}
if (adcd.length() == 15 && adcd.endsWith("000")) {
return adcd.substring(0, 12) + "%";
}
return adcd + "%";
}
/**
*
*
* @param adcd
* @return
*/
public static boolean isCounty(String adcd) {
if (StrKit.isBlank(adcd)) {
return false;
}
return (adcd.length() == 15 && adcd.endsWith("000000000")) || (adcd.length() == 12 && adcd.endsWith("000000"));
}
/**
*
*
* @param adcd
* @return
*/
public static boolean isTown(String adcd) {
if (StrKit.isBlank(adcd)) {
return false;
}
return (
adcd.length() == 15 && !adcd.endsWith("000000000") && adcd.endsWith("000000"))
|| (adcd.length() == 12 && !adcd.endsWith("000000") && adcd.endsWith("000")
);
}
/**
*
*
* @param adcd
* @return
*/
public static boolean isVillage(String adcd) {
if (StrKit.isBlank(adcd)) {
return false;
}
return (
adcd.length() == 15 && !adcd.endsWith("000000000") && !adcd.endsWith("000000") && adcd.endsWith("000"))
|| (adcd.length() == 12 && !adcd.endsWith("000000") && !adcd.endsWith("000")
);
}
/**
*
*
* @param adcd
* @return
*/
public static boolean isNaturalVillage(String adcd) {
if (StrKit.isBlank(adcd) || adcd.length() != 15) {
return false;
}
return !adcd.endsWith("000");
}
public static boolean isPhone(String phone) {
return Pattern.matches(regphone, phone);
}
/**
* sql
* @param table
* @param columns
* @return
*/
public static String findByColumnsSql(String table, String... columns) {
StringBuilder sql = new StringBuilder("select * from `");
sql.append(table);
sql.append("` where ");
for (int i=0; i<columns.length; i++) {
if (i > 0) {
sql.append(" and ");
}
sql.append('`').append(columns[i]).append("` = ?");
}
return sql.toString();
}
/**
*
* @param table
* @param columns
* @return
*/
public static String findFirstByColumnsSql(String table, String... columns) {
return findByColumnsSql(table, columns) + " limit 0,1";
}
/**
*
*
* @param str
*
* @return
*/
public static String[] strToStrArray(String str) {
return strToStrArrayManager(str, DEFAULT_PATH_SEPARATOR);
}
/**
* separator
*
* @param str
* @param separator
* @return
*/
public static String[] strToStrArray(String str, String separator) {
return strToStrArrayManager(str, separator);
}
private static String[] strToStrArrayManager(String str, String separator) {
StringTokenizer strTokens = new StringTokenizer(str, separator);
String[] strArray = new String[strTokens.countTokens()];
int i = 0;
while (strTokens.hasMoreTokens()) {
strArray[i] = strTokens.nextToken().trim();
i++;
}
return strArray;
}
public static String getRecordStr(Record record, String key){
String out = record.getStr(key);
if(out == null){
return "";
}
return out;
}
public static boolean validPhone(String phone){
if(StrKit.isBlank(phone)){
return false;
}
Pattern pattern = Pattern.compile(StrUtil.regphone);
Matcher matcher = pattern.matcher(phone);
return matcher.matches();
}
public static String join(Collection var0, String var1) {
StringBuffer var2 = new StringBuffer();
for(Iterator var3 = var0.iterator(); var3.hasNext(); var2.append((String)var3.next())) {
if (var2.length() != 0) {
var2.append(var1);
}
}
return var2.toString();
}
}