simplestringtokenizer.java
来自「cqME :java framework for TCK test.」· Java 代码 · 共 184 行
JAVA
184 行
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.midp.javatest.agent;import java.util.Vector;/** * Utility class to split strings. * Simple workaround for absence of method String.split in CLDC. * There is two main ways of usage:<ul> * <li> create instance of SimpleStringTokenizer * and use {@link #nextToken}, {@link #hasMoreTokens}.</li> * <li> use static methods {@link #split(String, char)}, * {@link #split(String, char[])}, * {@link #split(String, String)}, * {@link #split(String, String[])}. </li> * </ul> */class SimpleStringTokenizer { private String string; private String[] separators; private int start_pos; private int end_pos; private int cur_sep_length; /** * Creates instance of SimpleStringTokenizer. * @param string input string to parse. * @param separator separator character. */ public SimpleStringTokenizer(String string, char separator) { this(string, new Character(separator).toString()); } /** * Creates instance of SimpleStringTokenizer. * @param string input string to parse. * @param separator separator string. */ public SimpleStringTokenizer(String string, String separator) { this.string = string; this.separators = new String [] {separator}; reset(); } /** * Creates instance of SimpleStringTokenizer. * @param string input string to parse. * @param separators array of separator strings. */ public SimpleStringTokenizer(String string, String [] separators) { this.string = string; this.separators = separators; reset(); } /** * Gets next token. * @return next token. */ public String nextToken() { start_pos = end_pos + cur_sep_length; end_pos = string.length(); for (int i = 0; i != separators.length; ++i) { int index = string.indexOf(separators[i], start_pos); if (index == -1) { index = string.length(); } if (index < end_pos) { end_pos = index; cur_sep_length = separators[i].length(); } } return string.substring(start_pos, end_pos); } /** * Checks if there are any tokens more. * @return true if if there are any tokens more. */ public boolean hasMoreTokens() { return string.length() != 0 && end_pos != string.length(); } /** * Resets to initial state. */ public void reset() { start_pos = 0; end_pos = -1; cur_sep_length = 1; } /** * Splits <code>str</code> using <code>sep</code> as separator. * @param str input string to parse. * @param sep separator character. * @return array of resulting strings. */ public static String [] split(String str, char sep) { return split(str, new Character(sep).toString()); } /** * Splits <code>str</code> using <code>sep</code> as separators. * @param str input string to parse. * @param sep array of separator characters. * @return array of resulting strings. */ public static String [] split(String str, char [] sep) { String [] sep_str = new String[sep.length]; for (int i = 0; i != sep.length; ++i) { sep_str[i] = new Character(sep[i]).toString(); } return split(str, sep_str); } /** * Splits <code>str</code> using <code>sep</code> as separator. * @param str input string to parse. * @param sep separator string. * @return array of resulting strings. */ public static String [] split(String str, String sep) { return split(str, new String[] {sep}); } /** * Splits <code>str</code> using <code>sep</code> as separators. * @param str input string to parse. * @param sep array of separator strings. * @return array of resulting strings. */ public static String [] split(String str, String [] sep) { Vector vect = new Vector(); for (SimpleStringTokenizer t = new SimpleStringTokenizer(str, sep); t.hasMoreTokens();) { String token = t.nextToken(); if (token.length() != 0) { vect.addElement(token); } } String [] res = new String [vect.size()]; for (int i = 0; i != vect.size(); ++i) { res[i] = (String) vect.elementAt(i); } return res; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?