maven包:
spring pom.xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
調用StringUtils方法
StringUtils.ordinalIndexOf("charging/cloud/SN22010001067/product/genren",".",2);
這句話的意思是:獲取”charging/cloud/SN22010001067/product/genren”中第2個“/”的位置
這樣我們就很容易就可以獲取到第2個“/”與第3個“/”之間的字符串:例
String topic = "charging/cloud/SN22010001067/product/genren";
String sn = topicToSn(topic );
public String topicToSn(String topic){
/*獲取第二個/與第三個/之間的字符串為sn*/
Integer begin = StringUtils.ordinalIndexOf(topic,"/",2);;
Integer end = StringUtils.ordinalIndexOf(topic,"/",3);
String sn = topic.substring(begin+1,end);
return sn;
}
后面使用字符串的處理類(StringUtils),來處理字符串就更加方便了
//縮短到某長度,用...結尾.其實就是(substring(str, 0, max-3) + "...")
//public static String abbreviate(String str,int maxWidth)
StringUtils.abbreviate("abcdefg", 6);// ---"abc..."
//字符串結尾的后綴是否與你要結尾的后綴匹配,若不匹配則添加后綴
StringUtils.appendIfMissing("abc","xyz");//---"abcxyz"
StringUtils.appendIfMissingIgnoreCase("abcXYZ","xyz");//---"abcXYZ"
//首字母大小寫轉換
StringUtils.capitalize("cat");//---"Cat"
StringUtils.uncapitalize("Cat");//---"cat"
//字符串擴充至指定大小且居中(若擴充大小少於原字符大小則返回原字符,若擴充大小為 負數則為0計算 )
StringUtils.center("abcd", 2);//--- "abcd"
StringUtils.center("ab", -1);//--- "ab"
StringUtils.center("ab", 4);//---" ab "
StringUtils.center("a", 4, "yz");//---"yayz"
StringUtils.center("abc", 7, "");//---" abc "
//去除字符串中的"\n", "\r", or "\r\n"
StringUtils.chomp("abc\r\n");//---"abc"
//判斷一字符串是否包含另一字符串
StringUtils.contains("abc", "z");//---false
StringUtils.containsIgnoreCase("abc", "A");//---true
//統計一字符串在另一字符串中出現次數
StringUtils.countMatches("abba", "a");//---2
//刪除字符串中的梭有空格
StringUtils.deleteWhitespace(" ab c ");//---"abc"
//比較兩字符串,返回不同之處。確切的說是返回第二個參數中與第一個參數所不同的字符串
StringUtils.difference("abcde", "abxyz");//---"xyz"
//檢查字符串結尾后綴是否匹配
StringUtils.endsWith("abcdef", "def");//---true
StringUtils.endsWithIgnoreCase("ABCDEF", "def");//---true
StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
//檢查起始字符串是否匹配
StringUtils.startsWith("abcdef", "abc");//---true
StringUtils.startsWithIgnoreCase("ABCDEF", "abc");//---true
StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"});//---true
//判斷兩字符串是否相同
StringUtils.equals("abc", "abc");//---true
StringUtils.equalsIgnoreCase("abc", "ABC");//---true
//比較字符串數組內的所有元素的字符序列,起始一致則返回一致的字符串,若無則返回""
StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"});//---"ab"
//正向查找字符在字符串中第一次出現的位置
StringUtils.indexOf("aabaabaa", "b");//---2
StringUtils.indexOf("aabaabaa", "b", 3);//---5(從角標3后查找)
StringUtils.ordinalIndexOf("aabaabaa", "a", 3);//---1(查找第n次出現的位置)
//反向查找字符串第一次出現的位置
StringUtils.lastIndexOf("aabaabaa", ‘b‘);//---5
StringUtils.lastIndexOf("aabaabaa", ‘b‘, 4);//---2
StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2);//---1
//判斷字符串大寫、小寫
StringUtils.isAllUpperCase("ABC");//---true
StringUtils.isAllLowerCase("abC");//---false
//判斷是否為空(注:isBlank與isEmpty 區別)
StringUtils.isBlank(null);StringUtils.isBlank("");StringUtils.isBlank(" ");//---true
StringUtils.isNoneBlank(" ", "bar");//---false
StringUtils.isEmpty(null);StringUtils.isEmpty("");//---true
StringUtils.isEmpty(" ");//---false
StringUtils.isNoneEmpty(" ", "bar");//---true
//判斷字符串數字
StringUtils.isNumeric("123");//---false
StringUtils.isNumeric("12 3");//---false (不識別運算符號、小數點、空格……)
StringUtils.isNumericSpace("12 3");//---true
//數組中加入分隔符號
//StringUtils.join([1, 2, 3], ‘;‘);//---"1;2;3"
//大小寫轉換
StringUtils.upperCase("aBc");//---"ABC"
StringUtils.lowerCase("aBc");//---"abc"
StringUtils.swapCase("The dog has a BONE");//---"tHE DOG HAS A bone"
//替換字符串內容……(replacePattern、replceOnce)
StringUtils.replace("aba", "a", "z");//---"zbz"
StringUtils.overlay("abcdef", "zz", 2, 4);//---"abzzef"(指定區域)
StringUtils.replaceEach("abcde", new String[]{"ab", "d"},
new String[]{"w", "t"});//---"wcte"(多組指定替換ab->w,d->t)
//重復字符
StringUtils.repeat(‘e‘, 3);//---"eee"
//反轉字符串
StringUtils.reverse("bat");//---"tab"
//刪除某字符
StringUtils.remove("queued", ‘u‘);//---"qeed"
//分割字符串
StringUtils.split("a..b.c", ‘.‘);//---["a", "b", "c"]
StringUtils.split("ab:cd:ef", ":", 2);//---["ab", "cd:ef"]
StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2);//---["ab", "cd-!-ef"]
StringUtils.splitByWholeSeparatorPreserveAllTokens("ab::cd:ef", ":");//-["ab"," ","cd","ef"]
//去除首尾空格,類似trim……(stripStart、stripEnd、stripAll、stripAccents)
StringUtils.strip(" ab c ");//---"ab c"
StringUtils.stripToNull(null);//---null
StringUtils.stripToEmpty(null);//---""
//截取字符串
StringUtils.substring("abcd", 2);//---"cd"
StringUtils.substring("abcdef", 2, 4);//---"cd"
//left、right從左(右)開始截取n位字符
StringUtils.left("abc", 2);//---"ab"
StringUtils.right("abc", 2);//---"bc"
//從第n位開始截取m位字符 n m
StringUtils.mid("abcdefg", 2, 4);//---"cdef"
StringUtils.substringBefore("abcba", "b");//---"a"
StringUtils.substringBeforeLast("abcba", "b");//---"abc"
StringUtils.substringAfter("abcba", "b");//---"cba"
StringUtils.substringAfterLast("abcba", "b");//---"a"
StringUtils.substringBetween("tagabctag", "tag");//---"abc"
StringUtils.substringBetween("yabczyabcz", "y", "z");//---"abc"
隨機數生成類(RandomStringUtils)
//從數組中選出最大值
NumberUtils.max(new int[] { 1, 2, 3, 4 });//---4
//判斷字符串是否全是整數
NumberUtils.isDigits("153.4");//--false
//判斷字符串是否是有效數字
NumberUtils.isNumber("0321.1");//---false
數組類 ArrayUtils
//創建數組
String[] array = ArrayUtils.toArray("1", "2");
//判斷兩個數據是否相等,如果內容相同, 順序相同 則返回 true
ArrayUtils.isEquals(arr1,arr2);
//判斷數組中是否包含某一對象
ArrayUtils.contains(arr, "33");
//二維數組轉換成MAP
Map map = ArrayUtils.toMap(new String[][] {
{ "RED", "#FF0000" }, { "GREEN", "#00FF00" }, { "BLUE", "#0000FF" } });
日期類DateUtils
//日期加n天
DateUtils.addDays(new Date(), n);
//判斷是否同一天
DateUtils.isSameDay(date1, date2);
//字符串時間轉換為Date
DateUtils.parseDate(str, parsePatterns);
附: StringUtile
/*
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.funtl.leesite.common.utils;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.LocaleResolver;
/**
* 字符串工具類, 繼承org.apache.commons.lang3.StringUtils類
*
* @author Lusifer
* @version 2013-05-22
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final char SEPARATOR = '_';
private static final String CHARSET_NAME = "UTF-8";
/**
* 轉換為字節數組
*
* @param str
* @return
*/
public static byte[] getBytes(String str) {
if (str != null) {
try {
return str.getBytes(CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
return null;
}
} else {
return null;
}
}
/**
* 轉換為字節數組
*
* @param str
* @return
*/
public static String toString(byte[] bytes) {
try {
return new String(bytes, CHARSET_NAME);
} catch (UnsupportedEncodingException e) {
return EMPTY;
}
}
/**
* 是否包含字符串
*
* @param str 驗證字符串
* @param strs 字符串組
* @return 包含返回true
*/
public static boolean inString(String str, String... strs) {
if (str != null) {
for (String s : strs) {
if (str.equals(trim(s))) {
return true;
}
}
}
return false;
}
/**
* 替換掉HTML標簽方法
*/
public static String replaceHtml(String html) {
if (isBlank(html)) {
return "";
}
String regEx = "<.+?>";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(html);
String s = m.replaceAll("");
return s;
}
/**
* 替換為手機識別的HTML,去掉樣式及屬性,保留回車。
*
* @param html
* @return
*/
public static String replaceMobileHtml(String html) {
if (html == null) {
return "";
}
return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>");
}
/**
* 替換為手機識別的HTML,去掉樣式及屬性,保留回車。
*
* @param txt
* @return
*/
public static String toHtml(String txt) {
if (txt == null) {
return "";
}
return replace(replace(Encodes.escapeHtml(txt), "\n", "<br/>"), "\t", " ");
}
/**
* 縮略字符串(不區分中英文字符)
*
* @param str 目標字符串
* @param length 截取長度
* @return
*/
public static String abbr(String str, int length) {
if (str == null) {
return "";
}
try {
StringBuilder sb = new StringBuilder();
int currentLength = 0;
for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
currentLength += String.valueOf(c).getBytes("GBK").length;
if (currentLength <= length - 3) {
sb.append(c);
} else {
sb.append("...");
break;
}
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
public static String abbr2(String param, int length) {
if (param == null) {
return "";
}
StringBuffer result = new StringBuffer();
int n = 0;
char temp;
boolean isCode = false; // 是不是HTML代碼
boolean isHTML = false; // 是不是HTML特殊字符,如
for (int i = 0; i < param.length(); i++) {
temp = param.charAt(i);
if (temp == '<') {
isCode = true;
} else if (temp == '&') {
isHTML = true;
} else if (temp == '>' && isCode) {
n = n - 1;
isCode = false;
} else if (temp == ';' && isHTML) {
isHTML = false;
}
try {
if (!isCode && !isHTML) {
n += String.valueOf(temp).getBytes("GBK").length;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (n <= length - 3) {
result.append(temp);
} else {
result.append("...");
break;
}
}
// 取出截取字符串中的HTML標記
String temp_result = result.toString().replaceAll("(>)[^<>]*(<?)", "$1$2");
// 去掉不需要結素標記的HTML標記
temp_result = temp_result.replaceAll("</?(AREA|BASE|BASEFONT|BODY|BR|COL|COLGROUP|DD|DT|FRAME|HEAD|HR|HTML|IMG|INPUT|ISINDEX|LI|LINK|META|OPTION|P|PARAM|TBODY|TD|TFOOT|TH|THEAD|TR|area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>", "");
// 去掉成對的HTML標記
temp_result = temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>", "$2");
// 用正則表達式取出標記
Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
Matcher m = p.matcher(temp_result);
List<String> endHTML = Lists.newArrayList();
while (m.find()) {
endHTML.add(m.group(1));
}
// 補全不成對的HTML標記
for (int i = endHTML.size() - 1; i >= 0; i--) {
result.append("</");
result.append(endHTML.get(i));
result.append(">");
}
return result.toString();
}
/**
* 轉換為Double類型
*/
public static Double toDouble(Object val) {
if (val == null) {
return 0D;
}
try {
return Double.valueOf(trim(val.toString()));
} catch (Exception e) {
return 0D;
}
}
/**
* 轉換為Float類型
*/
public static Float toFloat(Object val) {
return toDouble(val).floatValue();
}
/**
* 轉換為Long類型
*/
public static Long toLong(Object val) {
return toDouble(val).longValue();
}
/**
* 轉換為Integer類型
*/
public static Integer toInteger(Object val) {
return toLong(val).intValue();
}
/**
* 獲得i18n字符串
*/
public static String getMessage(String code, Object[] args) {
LocaleResolver localLocaleResolver = (LocaleResolver) SpringContextHolder.getBean(LocaleResolver.class);
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Locale localLocale = localLocaleResolver.resolveLocale(request);
return SpringContextHolder.getApplicationContext().getMessage(code, args, localLocale);
}
/**
* 獲得用戶遠程地址
*/
public static String getRemoteAddr(HttpServletRequest request) {
String remoteAddr = request.getHeader("X-Real-IP");
if (isNotBlank(remoteAddr)) {
remoteAddr = request.getHeader("X-Forwarded-For");
} else if (isNotBlank(remoteAddr)) {
remoteAddr = request.getHeader("Proxy-Client-IP");
} else if (isNotBlank(remoteAddr)) {
remoteAddr = request.getHeader("WL-Proxy-Client-IP");
}
return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
}
/**
* 駝峰命名法工具
*
* @return toCamelCase("hello_world") == "helloWorld"
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
* toUnderScoreCase("helloWorld") = "hello_world"
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 駝峰命名法工具
*
* @return toCamelCase("hello_world") == "helloWorld"
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
* toUnderScoreCase("helloWorld") = "hello_world"
*/
public static String toCapitalizeCamelCase(String s) {
if (s == null) {
return null;
}
s = toCamelCase(s);
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
/**
* 駝峰命名法工具
*
* @return toCamelCase("hello_world") == "helloWorld"
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
* toUnderScoreCase("helloWorld") = "hello_world"
*/
public static String toUnderScoreCase(String s) {
if (s == null) {
return null;
}
StringBuilder sb = new StringBuilder();
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
boolean nextUpperCase = true;
if (i < (s.length() - 1)) {
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
}
if ((i > 0) && Character.isUpperCase(c)) {
if (!upperCase || !nextUpperCase) {
sb.append(SEPARATOR);
}
upperCase = true;
} else {
upperCase = false;
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
/**
* 如果不為空,則設置值
*
* @param target
* @param source
*/
public static void setValueIfNotBlank(String target, String source) {
if (isNotBlank(source)) {
target = source;
}
}
/**
* 轉換為JS獲取對象值,生成三目運算返回結果
*
* @param objectString 對象串
* 例如:row.user.id
* 返回:!row?'':!row.user?'':!row.user.id?'':row.user.id
*/
public static String jsGetVal(String objectString) {
StringBuilder result = new StringBuilder();
StringBuilder val = new StringBuilder();
String[] vals = split(objectString, ".");
for (int i = 0; i < vals.length; i++) {
val.append("." + vals[i]);
result.append("!" + (val.substring(1)) + "?'':");
}
result.append(val.substring(1));
return result.toString();
}
/**
* 通過正則表達式獲取內容
*
* @param regex 正則表達式
* @param from 原字符串
* @return
*/
public static String[] regex(String regex, String from) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(from);
List<String> results = new ArrayList<String>();
while (matcher.find()) {
for (int i = 0; i < matcher.groupCount(); i++) {
results.add(matcher.group(i + 1));
}
}
return results.toArray(new String[]{});
}
}
