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}$"; // 放的比较宽 public static final String regphone_tel = "^((0\\d{2,3}-\\d{7,8})|(1[3584]\\d{9}))$"; // 座机或者手机 public static final String DEFAULT_PATH_SEPARATOR = ","; /** * 根据传入的 adcd 截取到对应的行政区划 * 例如,传入村,保留前12位,传入镇,保留前9位,以此类推 * * @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 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(); } }