¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.terra.coal.helper; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * StringHelper |
| | | * @author WWW |
| | | */ |
| | | public class StringHelper { |
| | | /** |
| | | * æ°åæ£å |
| | | */ |
| | | public static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?"); |
| | | |
| | | /** |
| | | * æ ¼å¼åå½åç³»ç»æ¥æ 1 |
| | | */ |
| | | public static final SimpleDateFormat YMD_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); |
| | | |
| | | /** |
| | | * æ ¼å¼åå½åç³»ç»æ¥æ 2 |
| | | */ |
| | | public static final SimpleDateFormat YMDHMS_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | /** |
| | | * æ ¼å¼åå½åç³»ç»æ¥æ 3 |
| | | */ |
| | | public static final SimpleDateFormat YMD2_FORMAT = new SimpleDateFormat("yyyyMMdd"); |
| | | |
| | | /** |
| | | * æ ¼å¼åå½åç³»ç»æ¥æ 4 |
| | | */ |
| | | public static final SimpleDateFormat YMDHMS2_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | |
| | | /** |
| | | * 夿å符串,æ¯å¦ä¸ºæ´æ° |
| | | */ |
| | | public static boolean isInteger(String str) { |
| | | return str != null && str.matches("[0-9]+"); |
| | | } |
| | | |
| | | /** |
| | | * 夿å符串,æ¯å¦ä¸ºæµ®ç¹æ° |
| | | */ |
| | | public static boolean isNumeric(String str) { |
| | | return str != null && str.matches("-?\\d+(\\.\\d+)?"); |
| | | } |
| | | |
| | | /** |
| | | * 夿å符串,æ¯å¦ä¸ºæµ®ç¹æ° |
| | | */ |
| | | public static boolean isNumeric2(String str) { |
| | | return str != null && NUMBER_PATTERN.matcher(str).matches(); |
| | | } |
| | | |
| | | /** |
| | | * æ¥ææ£å |
| | | */ |
| | | public static Pattern datePattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/]((((0?[13578])|(1[02]))[\\-\\/]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/]((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/]((((0?[13578])|(1[02]))[\\-\\/]((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/]((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/]((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$"); |
| | | |
| | | /** |
| | | * SQLæ£å |
| | | */ |
| | | public static Pattern sqlPattern = Pattern.compile("|and|exec|execute|insert|select|delete|update|count|drop|\\*|%|chr|mid|master|truncate|char|declare|sitename|net user|xp_cmdshell|;|or|-|\\+|,|like"); |
| | | |
| | | /** |
| | | * åç¬¦ä¸²è½¬ä¸ºæ¥æ |
| | | */ |
| | | public static Date parseDate(String str) { |
| | | try { |
| | | return YMD_FORMAT.parse(str); |
| | | } catch (Exception ex) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * åç¬¦ä¸²è½¬ä¸ºæ¥ææ¶é´ |
| | | */ |
| | | public static Date parseTime(String str) { |
| | | try { |
| | | return YMDHMS_FORMAT.parse(str); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 夿弿¯å¦ä¸ºæ¥ææ ¼å¼ |
| | | */ |
| | | public static boolean isDate(String strDate) { |
| | | Matcher m = datePattern.matcher(strDate); |
| | | |
| | | return m.matches(); |
| | | } |
| | | |
| | | /** |
| | | * åç¬¦ä¸²ï¼æ¯å¦ä¸ºnull æ "" |
| | | */ |
| | | public static boolean isNull(String str) { |
| | | return null == str || str.length() == 0; |
| | | } |
| | | |
| | | /** |
| | | * å符串,æ¯å¦ä¸ºç©ºnullåç©ºæ ¼ |
| | | */ |
| | | public static boolean isEmpty(String str) { |
| | | return null == str || "".equals(str.trim()); |
| | | } |
| | | |
| | | /** |
| | | * è·å like å符串 |
| | | */ |
| | | public static String getLikeStr(String str) { |
| | | return StringHelper.isEmpty(str) ? null : "%" + str.trim() + "%"; |
| | | } |
| | | |
| | | /** |
| | | * è·å like å符串 |
| | | */ |
| | | public static String getLikeUpperStr(String str) { |
| | | return StringHelper.isEmpty(str) ? null : "%" + str.trim().toUpperCase() + "%"; |
| | | } |
| | | |
| | | /** |
| | | * è·å å³like å符串 |
| | | */ |
| | | public static String getRightLike(String str) { |
| | | return StringHelper.isEmpty(str) ? null : str.trim() + "%"; |
| | | } |
| | | |
| | | /** |
| | | * è·åå¾å½¢çWKTå符串 |
| | | * |
| | | * @param wkt |
| | | * @return |
| | | */ |
| | | public static String getGeomWkt(String wkt) { |
| | | if (StringHelper.isEmpty(wkt)) { |
| | | return "null"; |
| | | } |
| | | |
| | | return String.format("ST_GeomFromText('%s')", wkt); |
| | | } |
| | | |
| | | /** |
| | | * é¦åæ¯å¤§å |
| | | */ |
| | | public static String firstCharToUpperCase(String str) { |
| | | return str.substring(0, 1).toUpperCase() + str.substring(1); |
| | | } |
| | | |
| | | /** |
| | | * é¦åæ¯å°å |
| | | */ |
| | | public static String firstCharToLowerCase(String str) { |
| | | return str.substring(0, 1).toLowerCase() + str.substring(1); |
| | | } |
| | | |
| | | /** |
| | | * 夿弿¯å¦åå¨SQL注å
¥ |
| | | * |
| | | * @param str å符串 |
| | | * @return æ¯/å¦ |
| | | */ |
| | | public static boolean isSqlInjection(String str) { |
| | | if (null == str) { |
| | | return false; |
| | | } |
| | | |
| | | Matcher m = sqlPattern.matcher(str); |
| | | |
| | | return m.matches(); |
| | | } |
| | | |
| | | /** |
| | | * è·åGUID |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getGuid() { |
| | | return UUID.randomUUID().toString(); |
| | | } |
| | | |
| | | /** |
| | | * è¿æ¥Listéå |
| | | * |
| | | * @param list list æ´æ°éå |
| | | * @param join join è¿æ¥å符 |
| | | * @param <T> æ³åç±» |
| | | * @return å符串 |
| | | */ |
| | | public static <T> String join(List<T> list, String join) { |
| | | if (null == list || list.isEmpty()) { |
| | | return ""; |
| | | } |
| | | |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (T t : list) { |
| | | if (null != t) { |
| | | sb.append(t.toString() + join); |
| | | } |
| | | } |
| | | |
| | | if (sb.length() > 0 && sb.lastIndexOf(join) == sb.length() - join.length()) { |
| | | // å é¤ä»ç´¢å¼ start å¼å§å° end ä¹é´çå符ï¼å³ åå
æ¬ åä¸å
æ¬ã |
| | | sb.delete(sb.length() - join.length(), sb.length()); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } |
| | | } |