⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutil.java.svn-base

📁 這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2000-2003 Board of Trustees of Leland Stanford Jr. University, all rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the name of Stanford University shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Stanford University. */package base;import java.util.*;import java.io.*;import java.lang.reflect.*;/** * This is a class to contain generic string utilities *  * @author Thomas S. Robertson * @version 0.0 */public class StringUtil {	/**	 * Replace all occurances of oldstr in line with newstr	 * 	 * @param line	 *            string to be modified	 * @param oldstr	 *            string to be replace	 * @param newstr	 *            string to replace oldstr	 * @return new string with oldstr replaced by newstr	 */	public static String replaceString(String line, String oldstr, String newstr) {		int oldLen = oldstr.length();		if (oldLen == 0 || oldstr.equals(newstr)) {			return line;		}		int lineLen = line.length();		StringBuffer sb = new StringBuffer(lineLen);		int oldIdx = 0;		int thisIdx;		while ((thisIdx = line.indexOf(oldstr, oldIdx)) >= 0) {			for (int ix = oldIdx; ix < thisIdx; ix++) {				sb.append(line.charAt(ix));			}			sb.append(newstr);			oldIdx = thisIdx + oldLen;		}		for (int ix = oldIdx; ix < lineLen; ix++) {			sb.append(line.charAt(ix));		}		return sb.toString();	}	public static String replaceFirst(String line, String oldstr, String newstr) {		int oldLen = oldstr.length();		if (oldLen == 0 || oldstr.equals(newstr)) {			return line;		}		int lineLen = line.length();		StringBuffer sb = new StringBuffer(lineLen);		int index = line.indexOf(oldstr);		if (index < 0) {			return line;		} else {			sb.append(line.substring(0, index));			sb.append(newstr);			if (index + oldLen < line.length()) {				sb.append(line.substring(index + oldLen));			}		}		return sb.toString();	}	/**	 * Concatenate elements of collection into string, with separators	 * 	 * @param c -	 *            Collection of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @return Concatenated string	 */	public static String separatedString(Collection c, String separator) {		return separatedString(c, "", separator, "", new StringBuffer())				.toString();	}	/**	 * Concatenate elements of object array into string, with separators	 * 	 * @param arr -	 *            Array of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @return Concatenated string	 */	public static String separatedString(Object arr[], String separator) {		return separatedString(ListUtil.fromArray(arr), "", separator, "",				new StringBuffer()).toString();	}	/**	 * Concatenate elements of collection into string, with separators	 * 	 * @param c -	 *            Collection of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @param sb -	 *            StringBuffer to write result into	 * @return sb	 */	public static StringBuffer separatedString(Collection c, String separator,			StringBuffer sb) {		return separatedString(c, "", separator, "", sb);	}	/**	 * Concatenate elements of collection into string, delimiting each element,	 * adding separators	 * 	 * @param c -	 *            Collection of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @param delimiter -	 *            String with which to surround each element	 * @return Concatenated string	 */	public static String separatedDelimitedString(Collection c,			String separator, String delimiter) {		return separatedString(c, delimiter, delimiter + separator + delimiter,				delimiter, new StringBuffer()).toString();	}	/**	 * Concatenate elements of collection into string, delimiting each element,	 * adding separators	 * 	 * @param c -	 *            Collection of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @param delimiter1 -	 *            String with which to prefix each element	 * @param delimiter2 -	 *            String with which to suffix each element	 * @return Concatenated string	 */	public static String separatedDelimitedString(Collection c,			String separator, String delimiter1, String delimiter2) {		return separatedString(c, delimiter1,				delimiter2 + separator + delimiter1, delimiter2,				new StringBuffer()).toString();	}	/**	 * Concatenate elements of collection into string, adding separators,	 * terminating with terminator	 * 	 * @param c -	 *            Collection of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @param terminator -	 *            String with which to terminate result	 * @return Concatenated string	 */	public static String terminatedSeparatedString(Collection c,			String separator, String terminator) {		return separatedString(c, "", separator, terminator, new StringBuffer())				.toString();	}	/**	 * Concatenate elements of collection into string, adding separators,	 * delimitig each element	 * 	 * @param c -	 *            Collection of object (on which toString() will be called)	 * @param separator -	 *            String to put between elements	 * @param separatorFirst -	 *            String to place before first element	 * @param separatorInner -	 *            String with which to separate elements	 * @param separatorLast -	 *            String to place after last element	 * @return Concatenated string	 */	public static StringBuffer separatedString(Collection c,			String separatorFirst, String separatorInner, String separatorLast,			StringBuffer sb) {		if (c == null) {			return sb;		}		Iterator iter = c.iterator();		boolean first = true;		while (iter.hasNext()) {			if (first) {				first = false;				sb.append(separatorFirst);			} else {				sb.append(separatorInner);			}			Object obj = iter.next();			sb.append(obj == null ? "(null)" : obj.toString());		}		if (!first) {			sb.append(separatorLast);		}		return sb;	}	/**	 * Break a string at a separator char, returning a vector of at most	 * maxItems strings.	 * 	 * @param discardEmptyStrings	 *            if true, empty strings (caused by delimiters at the start or	 *            end of the string, or adjacent delimiters) will not be	 *            included in the result.	 * @param trimEachString	 *            is true, each string in the result will be trim()ed	 */	public static Vector<String> breakAt(String s, char sep, int maxItems,			boolean discardEmptyStrings, boolean trimEachString) {		Vector<String> res = new Vector<String>();		int len;		if (s == null || (len = s.length()) == 0) {			return res;		}		if (maxItems <= 0) {			maxItems = Integer.MAX_VALUE;		}		for (int pos = 0; maxItems > 0; maxItems--) {			int end = s.indexOf(sep, pos);			if (end == -1) {				if (pos > len) {					break;				}				end = len;			}			if (!discardEmptyStrings || pos != end) {				String str = s.substring(pos, end);				if (trimEachString) {					str = str.trim();				}				if (!discardEmptyStrings || str.length() != 0) {					res.addElement(str);				}			}			pos = end + 1;		}		return res;	}	/**	 * Break a string at a separator char, returning a vector of at most	 * maxItems strings.	 * 	 * @param discardEmptyStrings	 *            if true, empty strings (caused by delimiters at the start or	 *            end of the string, or adjacent delimiters) will not be	 *            included in the result.	 */	public static Vector breakAt(String s, char sep, int maxItems,			boolean discardEmptyStrings) {		return breakAt(s, sep, maxItems, discardEmptyStrings, false);	}	/**	 * Break a string at a separator char, returning a vector of strings.	 * Include any empty strings in the result.	 */	public static Vector breakAt(String s, char sep) {		return breakAt(s, sep, 0);	}	/**	 * Break a string at a separator char, returning a vector of at most	 * maxItems strings. Include any empty strings in the result.	 */	public static Vector breakAt(String s, char sep, int maxItems) {		return breakAt(s, sep, maxItems, false);	}	/**	 * Method to trim the end off of a string as soon as it encounters any one	 * of the characters specified	 * 	 * @param str	 *            String to trim	 * @param chars	 *            String containing the chars to trim after	 * @return str turncated at the first occurance of any of the chars	 */	public static String trimAfterChars(String str, String chars) {		if (str == null) {			return null;		}		if (chars != null) {			for (int ix = 0; ix < str.length(); ix++) {				for (int jx = 0; jx < chars.length(); jx++) {					if (str.charAt(ix) == chars.charAt(jx)) {						return str.substring(0, ix);					}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -