java自定義封裝StringUtils常用工具類
自定義封裝StringUtils常用工具類,供大家參考,具體內(nèi)容如下
package com.demo.utils; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 字符串操作工具類 * @author dongyangyang * @Date 2016/12/28 23:12 * @Version 1.0 * */ public class StringUtils { /** * 首字母變小寫 * @param str * @return */ public static String firstCharToLowerCase(String str) { char firstChar = str.charAt(0); if (firstChar >= 'A' && firstChar <= 'Z') { char[] arr = str.toCharArray(); arr[0] += ('a' - 'A'); return new String(arr); } return str; } /** * 首字母變大寫 * @param str * @return */ public static String firstCharToUpperCase(String str) { char firstChar = str.charAt(0); if (firstChar >= 'a' && firstChar <= 'z') { char[] arr = str.toCharArray(); arr[0] -= ('a' - 'A'); return new String(arr); } return str; } /** * 判斷是否為空 * @param str * @return */ public static boolean isEmpty(final String str) { return (str == null) || (str.length() == 0); } /** * 判斷是否不為空 * @param str * @return */ public static boolean isNotEmpty(final String str) { return !isEmpty(str); } /** * 判斷是否空白 * @param str * @return */ public static boolean isBlank(final String str) { int strLen; if ((str == null) || ((strLen = str.length()) == 0)) return true; for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } /** * 判斷是否不是空白 * @param str * @return */ public static boolean isNotBlank(final String str) { return !isBlank(str); } /** * 判斷多個字符串全部是否為空 * @param strings * @return */ public static boolean isAllEmpty(String... strings) { if (strings == null) { return true; } for (String str : strings) { if (isNotEmpty(str)) { return false; } } return true; } /** * 判斷多個字符串其中任意一個是否為空 * @param strings * @return */ public static boolean isHasEmpty(String... strings) { if (strings == null) { return true; } for (String str : strings) { if (isEmpty(str)) { return true; } } return false; } /** * checkValue為 null 或者為 "" 時返回 defaultValue * @param checkValue * @param defaultValue * @return */ public static String isEmpty(String checkValue, String defaultValue) { return isEmpty(checkValue) ? defaultValue : checkValue; } /** * 字符串不為 null 而且不為 "" 并且等于other * @param str * @param other * @return */ public static boolean isNotEmptyAndEquelsOther(String str, String other) { if (isEmpty(str)) { return false; } return str.equals(other); } /** * 字符串不為 null 而且不為 "" 并且不等于other * @param str * @param other * @return */ public static boolean isNotEmptyAndNotEquelsOther(String str, String... other) { if (isEmpty(str)) { return false; } for (int i = 0; i < other.length; i++) { if (str.equals(other[i])) { return false; } } return true; } /** * 字符串不等于other * @param str * @param other * @return */ public static boolean isNotEquelsOther(String str, String... other) { for (int i = 0; i < other.length; i++) { if (other[i].equals(str)) { return false; } } return true; } /** * 判斷字符串不為空 * @param strings * @return */ public static boolean isNotEmpty(String... strings) { if (strings == null) { return false; } for (String str : strings) { if (str == null || "".equals(str.trim())) { return false; } } return true; } /** * 比較字符相等 * @param value * @param equals * @return */ public static boolean equals(String value, String equals) { if (isAllEmpty(value, equals)) { return true; } return value.equals(equals); } /** * 比較字符串不相等 * @param value * @param equals * @return */ public static boolean isNotEquals(String value, String equals) { return !equals(value, equals); } public static String[] split(String content, String separatorChars) { return splitWorker(content, separatorChars, -1, false); } public static String[] split(String str, String separatorChars, int max) { return splitWorker(str, separatorChars, max, false); } public static final String[] EMPTY_STRING_ARRAY = new String[0]; private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) { if (str == null) { return null; } int len = str.length(); if (len == 0) { return EMPTY_STRING_ARRAY; } List<String> list = new ArrayList<String>(); int sizePlus1 = 1; int i = 0, start = 0; boolean match = false; boolean lastMatch = false; if (separatorChars == null) { while (i < len) { if (Character.isWhitespace(str.charAt(i))) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } else if (separatorChars.length() == 1) { char sep = separatorChars.charAt(0); while (i < len) { if (str.charAt(i) == sep) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } else { while (i < len) { if (separatorChars.indexOf(str.charAt(i)) >= 0) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } if (match || (preserveAllTokens && lastMatch)) { list.add(str.substring(start, i)); } return (String[]) list.toArray(EMPTY_STRING_ARRAY); } /** * 消除轉(zhuǎn)義字符 * @param str * @return */ public static String escapeXML(String str) { if (str == null) return ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { char c = str.charAt(i); switch (c) { case '\u00FF': case '\u0024': break; case '&': sb.append("&"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '\"': sb.append("""); break; case '\'': sb.append("'"); break; default: if (c >= '\u0000' && c <= '\u001F') break; if (c >= '\uE000' && c <= '\uF8FF') break; if (c >= '\uFFF0' && c <= '\uFFFF') break; sb.append(c); break; } } return sb.toString(); } /** * 將字符串中特定模式的字符轉(zhuǎn)換成map中對應(yīng)的值 * * @param s * 需要轉(zhuǎn)換的字符串 * @param map * 轉(zhuǎn)換所需的鍵值對集合 * @return 轉(zhuǎn)換后的字符串 */ public static String replace(String s, Map<String, Object> map) { StringBuilder ret = new StringBuilder((int) (s.length() * 1.5)); int cursor = 0; for (int start, end; (start = s.indexOf("${", cursor)) != -1 && (end = s.indexOf("}", start)) != -1;) { ret.append(s.substring(cursor, start)).append(map.get(s.substring(start + 2, end))); cursor = end + 1; } ret.append(s.substring(cursor, s.length())); return ret.toString(); } public static String replace(String s, Object... objs) { if (objs == null || objs.length == 0) return s; if (s.indexOf("{}") == -1) return s; StringBuilder ret = new StringBuilder((int) (s.length() * 1.5)); int cursor = 0; int index = 0; for (int start; (start = s.indexOf("{}", cursor)) != -1;) { ret.append(s.substring(cursor, start)); if (index < objs.length) ret.append(objs[index]); else ret.append("{}"); cursor = start + 2; index++; } ret.append(s.substring(cursor, s.length())); return ret.toString(); } /** * 字符串格式化工具,參數(shù)必須以{0}之類的樣式標(biāo)示出來.大括號中的數(shù)字從0開始。 * * @param source * 源字符串 * @param params * 需要替換的參數(shù)列表,寫入時會調(diào)用每個參數(shù)的toString(). * @return 替換完成的字符串。如果原始字符串為空或者參數(shù)為空那么將直接返回原始字符串。 */ public static String replaceArgs(String source, Object... params) { if (params == null || params.length == 0 || source == null || source.isEmpty()) { return source; } StringBuilder buff = new StringBuilder(source); StringBuilder temp = new StringBuilder(); int startIndex = 0; int endIndex = 0; String param = null; for (int count = 0; count < params.length; count++) { if (params[count] == null) { param = null; } else { param = params[count].toString(); } temp.delete(0, temp.length()); temp.append("{"); temp.append(count); temp.append("}"); while (true) { startIndex = buff.indexOf(temp.toString(), endIndex); if (startIndex == -1) { break; } endIndex = startIndex + temp.length(); buff.replace(startIndex, endIndex, param == null ? "" : param); } startIndex = 0; endIndex = 0; } return buff.toString(); } public static String substringBefore(final String s, final String separator) { if (isEmpty(s) || separator == null) { return s; } if (separator.isEmpty()) { return ""; } final int pos = s.indexOf(separator); if (pos < 0) { return s; } return s.substring(0, pos); } public static String substringBetween(final String str, final String open, final String close) { if (str == null || open == null || close == null) { return null; } final int start = str.indexOf(open); if (start != -1) { final int end = str.indexOf(close, start + open.length()); if (end != -1) { return str.substring(start + open.length(), end); } } return null; } public static String substringAfter(final String str, final String separator) { if (isEmpty(str)) { return str; } if (separator == null) { return ""; } final int pos = str.indexOf(separator); if (pos == -1) { return ""; } return str.substring(pos + separator.length()); } /** * 轉(zhuǎn)換為字節(jié)數(shù)組 * @param str * @return */ public static String toString(byte[] bytes){ try { return new String(bytes, "utf-8"); } catch (UnsupportedEncodingException e) { return null; } } /** * 轉(zhuǎn)換為字節(jié)數(shù)組 * @param str * @return */ public static byte[] getBytes(String str){ if (str != null){ try { return str.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { return null; } }else{ return null; } } public static void main(String[] a){ String escapeXML = escapeXML("\\"); System.out.println(escapeXML); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一篇文章告訴你JAVA Mybatis框架的核心原理到底有多重要
yBatis的底層操作封裝了JDBC的API,MyBatis的工作原理以及核心流程與JDBC的使用步驟一脈相承,MyBatis的核心對象(SqlSession,Executor)與JDBC的核心對象(Connection,Statement)相互對應(yīng)2021-06-06Mybatis逆向工程實現(xiàn)連接MySQL數(shù)據(jù)庫
本文主要介紹了Mybatis逆向工程實現(xiàn)連接MySQL數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06詳解Java的Struts2框架的結(jié)構(gòu)及其數(shù)據(jù)轉(zhuǎn)移方式
這篇文章主要介紹了詳解Java的Struts2框架的結(jié)構(gòu)及其數(shù)據(jù)轉(zhuǎn)移方式,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2016-01-01ElasticSearch學(xué)習(xí)之Es索引Api操作
這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之Es索引Api操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01SpringBoot打jar包遇到的xml文件丟失的解決方案
這篇文章主要介紹了SpringBoot打jar包遇到的xml文件丟失的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Springcould多模塊搭建Eureka服務(wù)器端口過程詳解
這篇文章主要介紹了Springcould多模塊搭建Eureka服務(wù)器端口過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作實例代碼
這篇文章主要給大家介紹了關(guān)于Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11java實現(xiàn)ThreadLocal線程局部變量的實現(xiàn)
本文主要介紹了java實現(xiàn)ThreadLocal線程局部變量的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java輕松實現(xiàn)批量插入或刪除Excel行列操作
在職場生活中,對Excel工作表的行和列進(jìn)行操作是非常普遍的需求,下面小編就來和大家介紹一下如何在Java中完成批量插入、刪除行和列的操作吧2023-10-10