在J2ME中实现StringTokenizer

时间:2021-05-02

在se中用惯了的StringTokenizer,在me中没有了,我还急着用,于是在Google上搜索了一下,果然有高手已经解决了,把代码放在下面:

import java.util.*;

public class StringTokenizer implements Enumeration { private void setMaxDelimChar() { if(delimiters == null) {maxDelimChar = '\0';return; } char c = '\0'; for(int i = 0; i < delimiters.length(); i ){ char c1 = delimiters.charAt(i); if(c < c1) c = c1; } maxDelimChar = c; }

public StringTokenizer(String s, String s1, boolean flag){ currentPosition = 0;newPosition = -1; delimsChanged = false;str = s; maxPosition = s.length(); delimiters = s1; retDelims = flag;setMaxDelimChar(); }

public StringTokenizer(String s, String s1) { this(s, s1, false); }

public StringTokenizer(String s) { this(s, " \t\n\r\f", false); }

private int skipDelimiters(int i) { if(delimiters == null) throw new NullPointerException();int j;for(j = i; !retDelims && j < maxPosition; j ) { char c = str.charAt(j); if(c > maxDelimChar || delimiters.indexOf(c) < 0) break; } return j; }

private int scanToken(int i){ int j; for(j = i; j < maxPosition; j ) { char c = str.charAt(j); if(c <= maxDelimChar && delimiters.indexOf(c) >= 0) break; } if(retDelims && i == j) { char c1 = str.charAt(j);if(c1 <= maxDelimChar && delimiters.indexOf(c1) >= 0) j ; }return j; }

public boolean hasMoreTokens(){ newPosition = skipDelimiters(currentPosition); return newPosition < maxPosition; }

public String nextToken() { currentPosition = newPosition < 0 || delimsChanged ? skipDelimiters(currentPosition) : newPosition; delimsChanged = false; newPosition = -1; if(currentPosition >= maxPosition) { throw new NoSuchElementException(); } else{ int i = currentPosition; currentPosition = scanToken(currentPosition); return str.substring(i, currentPosition); } }

public String nextToken(String s){ delimiters = s; delimsChanged = true;setMaxDelimChar();return nextToken(); }

public boolean hasMoreElements() { return hasMoreTokens(); }

public Object nextElement() { return nextToken(); }

public int countTokens() { int i = 0; for(int j = currentPosition; j < maxPosition;) { j = skipDelimiters(j); if(j >= maxPosition) break;j = scanToken(j); i ; } return i; }

private int currentPosition; private int newPosition; private int maxPosition; private String str; private String delimiters; private boolean retDelims; private boolean delimsChanged; private char maxDelimChar;

}

本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章