Java中的StringTokenizer實現(xiàn)字符串切割詳解
前言
java.util工具包提供了字符串切割的工具類StringTokenizer,Spring等常見框架的字符串工具類(如Spring的StringUtils),常見此類使用。
例如Spring的StringUtils下的方法:
public static String[] tokenizeToStringArray( @Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) { if (str == null) { return EMPTY_STRING_ARRAY; } StringTokenizer st = new StringTokenizer(str, delimiters); List<String> tokens = new ArrayList<>(); while (st.hasMoreTokens()) { String token = st.nextToken(); if (trimTokens) { token = token.trim(); } if (!ignoreEmptyTokens || token.length() > 0) { tokens.add(token); } } return toStringArray(tokens); }
又如定時任務(wù)框架Quartz中,cron表達式類CronExpression,其中的buildExpression方法是為了處理cron表達式的,cron表達式有7個子表達式,空格隔開,cron表達式字符串的切割也使用到了StringTokenizer類,方法如下:
protected void buildExpression(String expression) throws ParseException { this.expressionParsed = true; try { if (this.seconds == null) { this.seconds = new TreeSet(); } if (this.minutes == null) { this.minutes = new TreeSet(); } if (this.hours == null) { this.hours = new TreeSet(); } if (this.daysOfMonth == null) { this.daysOfMonth = new TreeSet(); } if (this.months == null) { this.months = new TreeSet(); } if (this.daysOfWeek == null) { this.daysOfWeek = new TreeSet(); } if (this.years == null) { this.years = new TreeSet(); } int exprOn = 0; for(StringTokenizer exprsTok = new StringTokenizer(expression, " \t", false); exprsTok.hasMoreTokens() && exprOn <= 6; ++exprOn) { String expr = exprsTok.nextToken().trim(); if (exprOn == 3 && expr.indexOf(76) != -1 && expr.length() > 1 && expr.contains(",")) { throw new ParseException("Support for specifying 'L' and 'LW' with other days of the month is not implemented", -1); } if (exprOn == 5 && expr.indexOf(76) != -1 && expr.length() > 1 && expr.contains(",")) { throw new ParseException("Support for specifying 'L' with other days of the week is not implemented", -1); } if (exprOn == 5 && expr.indexOf(35) != -1 && expr.indexOf(35, expr.indexOf(35) + 1) != -1) { throw new ParseException("Support for specifying multiple \"nth\" days is not implemented.", -1); } StringTokenizer vTok = new StringTokenizer(expr, ","); while(vTok.hasMoreTokens()) { String v = vTok.nextToken(); this.storeExpressionVals(0, v, exprOn); } } if (exprOn <= 5) { throw new ParseException("Unexpected end of expression.", expression.length()); } else { if (exprOn <= 6) { this.storeExpressionVals(0, "*", 6); } TreeSet<Integer> dow = this.getSet(5); TreeSet<Integer> dom = this.getSet(3); boolean dayOfMSpec = !dom.contains(NO_SPEC); boolean dayOfWSpec = !dow.contains(NO_SPEC); if ((!dayOfMSpec || dayOfWSpec) && (!dayOfWSpec || dayOfMSpec)) { throw new ParseException("Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.", 0); } } } catch (ParseException var8) { throw var8; } catch (Exception var9) { throw new ParseException("Illegal cron expression format (" + var9.toString() + ")", 0); } }
使用方法
import com.google.common.collect.Lists; import java.util.List; import java.util.StringTokenizer; /** * @author xiaoxu * @date 2023-10-18 * spring_boot:com.xiaoxu.boot.tokenizer.TestStringTokenizer */ public class TestStringTokenizer { public static void main(String[] args) { print("你 好 嗎\t我是 \t你的\t 朋友 \t", " \t", false); } public static void print(String str, String delimiter, boolean isReturnDelims) { System.out.println("切割字符串:【" + str + "】;" + "分隔符:【" + delimiter + "】。"); List<String> strs = Lists.newArrayList(); String s; boolean x; for (StringTokenizer strToken = new StringTokenizer(str, delimiter, false); strToken.hasMoreTokens(); x = (s != null && strs.add(s))) { s = strToken.nextToken(); System.out.println("切割:【" + s + "】"); if(s.equals("嗎")) s = null; } System.out.println("字符串數(shù)組:" + strs); } }
執(zhí)行結(jié)果:
切割字符串:【你 好 嗎 我是 你的 朋友 】;分隔符:【 】。
切割:【你】
切割:【好】
切割:【嗎】
切割:【我是】
切割:【你的】
切割:【朋友】
字符串數(shù)組:[你, 好, 我是, 你的, 朋友]
源碼片段分析
public StringTokenizer(String str, String delim, boolean returnDelims) { currentPosition = 0; newPosition = -1; delimsChanged = false; this.str = str; maxPosition = str.length(); delimiters = delim; retDelims = returnDelims; setMaxDelimCodePoint(); }
private void setMaxDelimCodePoint() { if (delimiters == null) { maxDelimCodePoint = 0; return; } int m = 0; int c; int count = 0; for (int i = 0; i < delimiters.length(); i += Character.charCount(c)) { c = delimiters.charAt(i); if (c >= Character.MIN_HIGH_SURROGATE && c <= Character.MAX_LOW_SURROGATE) { c = delimiters.codePointAt(i); hasSurrogates = true; } if (m < c) m = c; count++; } maxDelimCodePoint = m; if (hasSurrogates) { delimiterCodePoints = new int[count]; for (int i = 0, j = 0; i < count; i++, j += Character.charCount(c)) { c = delimiters.codePointAt(j); delimiterCodePoints[i] = c; } } }
調(diào)用setMaxDelimCodePoint()方法,源碼可知,切割時設(shè)置int maxDelimCodePoint,是為了優(yōu)化分隔符的檢測(取的是分隔字符串中char的ASCII碼值最大的字符的ASCII值,存入maxDelimCodePoint中。在方法int scanToken(int startPos)中,若滿足條件(c <= maxDelimCodePoint) && (delimiters.indexOf© >= 0),意即該字符的ASCII碼值小于等于最大的maxDelimCodePoint,那么這個字符可能存在于分隔字符串中,再檢測delimiters分隔字符串中是否包含該字符,反之,若ASCII碼值大于分隔字符串中最大的maxDelimCodePoint,也就是說該字符一定不存在于分隔字符串里,&&直接跳過delimiters.indexOf的檢測,也就達到了優(yōu)化分隔符檢測的效果了)。
private int scanToken(int startPos) { int position = startPos; while (position < maxPosition) { if (!hasSurrogates) { char c = str.charAt(position); if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0)) break; position++; } else { int c = str.codePointAt(position); if ((c <= maxDelimCodePoint) && isDelimiter(c)) break; position += Character.charCount(c); } } if (retDelims && (startPos == position)) { if (!hasSurrogates) { char c = str.charAt(position); if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0)) position++; } else { int c = str.codePointAt(position); if ((c <= maxDelimCodePoint) && isDelimiter(c)) position += Character.charCount(c); } } return position; }
scanToken方法即跳過分隔字符串,只要某此循環(huán)時,該字符包含在分隔字符串里,那么position不再自增,以此時的position值作為實際切割獲取字符串的末索引, 因為subString方法是左閉右開的,該值是實際獲取字符串的末索引值+1,所以可以截取到完整的不包含分隔符的字符串片段。
skipDelimiters方法類似,即過濾連續(xù)包含于分隔字符串中的字符,獲取實際需要切割獲取的字符串的開始索引值。
private int skipDelimiters(int startPos) { if (delimiters == null) throw new NullPointerException(); int position = startPos; while (!retDelims && position < maxPosition) { if (!hasSurrogates) { char c = str.charAt(position); if ((c > maxDelimCodePoint) || (delimiters.indexOf(c) < 0)) break; position++; } else { int c = str.codePointAt(position); if ((c > maxDelimCodePoint) || !isDelimiter(c)) { break; } position += Character.charCount(c); } } return position; }
上述分析可知,只要待切割字符串中的字符,在分隔字符串中出現(xiàn),那么就會做一次切割(也就是不論分隔字符串中的每個char或字符串片段的順序,只要連續(xù)包含在分隔字符串里,就切割)。
演示如下(注意countTokens()方法不要在循環(huán)中和nextToken()一同使用):
public static void print2(String str, String delimiter, boolean isReturnDelims) { StringTokenizer strTokenizer = new StringTokenizer(str, delimiter); System.out.println("總數(shù)目:" + strTokenizer.countTokens()); int count; String[] strs = new String[count = strTokenizer.countTokens()]; // 注意:不要在循環(huán)里寫 int i = 0; i < strTokenizer.countTokens(); // 因為 countTokens方法需要使用currentPosition,而每次執(zhí)行nextToken方法時,currentPosition會一直往下偏移計算, // 會導(dǎo)致循環(huán)中, i < strTokenizer.countTokens();發(fā)生改變,這里應(yīng)該是常量總數(shù)目 for (int i = 0; i < count; i++) { String s = strTokenizer.nextToken(); strs[i] = s; } System.out.println(Arrays.toString(strs)); }
countTokens源碼如下:
public int countTokens() { int count = 0; int currpos = currentPosition; while (currpos < maxPosition) { currpos = skipDelimiters(currpos); if (currpos >= maxPosition) break; currpos = scanToken(currpos); count++; } return count; }
執(zhí)行:
print2("1a2b3c4ca5bc6ba7abc8acbbaba9", "abc", false);
結(jié)果如下所示:
總數(shù)目:9
[1, 2, 3, 4, 5, 6, 7, 8, 9]
到此這篇關(guān)于Java中的StringTokenizer實現(xiàn)字符串切割詳解的文章就介紹到這了,更多相關(guān)StringTokenizer字符串切割內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JSON.toJSONString()方法在Java中的使用方法及應(yīng)用場景
- Java中的String不可變性實現(xiàn)
- Java中的String、StringBuilder、StringBuffer三者的區(qū)別詳解
- Java的String類中的startsWith方法和endsWith方法示例詳解
- Java中的StringJoiner類使用示例深入詳解
- Java中的StringBuilder()常見方法詳解
- java8中的List<String>轉(zhuǎn)List<Integer>的實例代碼
- Java中如何取出String字符串括號中的內(nèi)容
- Java中的StringUtils引入及使用示例教程
- 深入理解Java中的String(示例詳解)
相關(guān)文章
Java大數(shù)運算BigInteger與進制轉(zhuǎn)換詳解
這篇文章主要介紹了Java大數(shù)運算BigInteger與進制轉(zhuǎn)換詳解,Java 提供了 BigInteger(大整數(shù))類和 BigDecimal(大浮點數(shù))類用于大數(shù)運算,這兩個類都繼承自 Number 類(抽象類),由于 BigInteger 在大數(shù)運算中更常見,需要的朋友可以參考下2023-09-09SpringBoot嵌入式Servlet容器與定制化組件超詳細講解
這篇文章主要介紹了SpringBoot嵌入式Servlet容器與定制化組件的使用介紹,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10