使用indexOf()和substring()来分析字符串容易导致StringIndexOutOfBoundsException。
StringTokenizer类来分析字符串则会容易一些,效率也会高一些。
StringTokenizer类
Java StringTokenizer类属于 java.util 包,用于分隔字符串。
StringTokenizer 构造方法:
- 1. StringTokenizer(String str) :构造一个用来解析 str 的 StringTokenizer 对象。java 默认的分隔符是空格("")、制表符( )、换行符( )、回车符( )。
- 2. StringTokenizer(String str, String delim) :构造一个用来解析 str 的 StringTokenizer 对象,并提供一个指定的分隔符。
- 3. StringTokenizer(String str, String delim, boolean returnDelims) :构造一个用来解析 str 的 StringTokenizer 对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。
源码:
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(); } public StringTokenizer(String str, String delim) { this(str, delim, false); } public StringTokenizer(String str) { this(str, " f", false); }
StringTokenizer 常用方法:
- 1. int countTokens():返回nextToken方法被调用的次数。
- 2. boolean hasMoreTokens():返回是否还有分隔符。
- 3. boolean hasMoreElements():判断枚举 (Enumeration) 对象中是否还有数据。
- 4. String nextToken():返回从当前位置到下一个分隔符的字符串。
- 5. Object nextElement():返回枚举 (Enumeration) 对象的下一个元素。
- 6. String nextToken(String delim):与 4 类似,以指定的分隔符返回结果。
源码:
//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; } //hasMoreTokens()********************************************** public boolean hasMoreTokens() { newPosition = skipDelimiters(currentPosition); return (newPosition < maxPosition); } 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; } //hasMoreElements()******************************************** public boolean hasMoreElements() { return hasMoreTokens(); } //nextToken()******************************************* public String nextToken() { currentPosition = (newPosition >= 0 && !delimsChanged) ? newPosition : skipDelimiters(currentPosition); delimsChanged = false; newPosition = -1; if (currentPosition >= maxPosition) throw new NoSuchElementException(); int start = currentPosition; currentPosition = scanToken(currentPosition); return str.substring(start, currentPosition); } public String nextToken(String delim) { delimiters = delim; delimsChanged = true; setMaxDelimCodePoint(); return nextToken(); } 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; } } } //nextElement()******************************************* public Object nextElement() { return nextToken(); }
实例:
实例1:
默认按照 f以及空格分割
public static void main(String[] args) { StringTokenizer str2 = new StringTokenizer("fsd fsd dgfsd fdsg"); System.out.println( str2.countTokens()); System.out.println("-----------------"); while(str2.hasMoreTokens()) { System.out.println(str2.nextToken()); System.out.println( str2.countTokens()); } }
结果:
4
-----------------
fsd
3
fsd
2
dgfsd
1
fdsg
0
实例2:
分割是按字符串中每个字符进行分割,并非按照字符串进行分割
public static void main(String[] args) { StringTokenizer str2 = new StringTokenizer("fsdfsddgfsdfdsg","sd"); System.out.println( str2.countTokens()); System.out.println("-----------------"); while(str2.hasMoreElements()) { System.out.println(str2.nextElement()); } System.out.println( str2.countTokens()); }
结果:
5
-----------------
f
f
gf
f
g
0
实例3:
ture表示返回分隔符
public static void main(String[] args) { StringTokenizer str2 = new StringTokenizer("fsdfsddgfsdfdsg","g",true); System.out.println( str2.countTokens()); System.out.println("-----------------"); while(str2.hasMoreElements()) { System.out.println(str2.nextElement()); } System.out.println( str2.countTokens()); }
结果:
4
-----------------
fsdfsdd
g
fsdfds
g
0
indexOf()方法
ndexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。从左向右执行查找。
substring()方法
substring() 方法返回字符串的子字符串。
1、public String substring(int beginIndex);
2、public String substring(int beginIndex, int endIndex);
beginIndex -- 起始索引(包括), 索引从 0 开始。
endIndex -- 结束索引(不包括)。