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

📄 stringutil.java

📁 struts+spring+hibernate自创框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.pegasus.framework.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

public class StringUtil {
	/**
	 * Private constructor to prevent instances of this class.
	 */
	private StringUtil() {
	}

	/**
	 * Checks if the field is <b>not</b> <code>null</code> and length of the field is greater than zero not including whitespace.
	 * 
	 * @param s
	 * The string to check.
	 * @return true if the field is null or blank.
	 */
	public static boolean isNullOrBlank(String s) {
		return (s == null || s.trim().length() == 0);
	}

	/**
	 * Checks if the field contains data - is <b>not</b> empty. The string is considered empty if it is null or just contains whitespace.
	 * 
	 * @param s
	 * The string to check.
	 * @return true if the field is contains data.
	 */
	public static boolean isNotEmpty(String s) {
		return !isNullOrBlank(s);
	}

	/**
	 * Gets the [index]'th substring from the String [s] using the seperator token [delim]. <p/>Example 1: <code>str = "abc;def;ghi;jkl", delim=";", index=2</code> will return <code>ghi</code>.
	 * <br/>Example 2 (no substring between two delimiters): <code>str = "abc;def;;jkl", delim=";", index=2</code> will return <code>jkl</code>. <p/>The index is zero based (starting from zero).
	 * <br/>Will return null if the index is out of bounds, or the seperator is not in the string.
	 * 
	 * @param str
	 * string.
	 * @param delim
	 * seperator token.
	 * @param index
	 * index of the sub element to get.
	 * @return the substring.
	 */
	public static String getSubString(String str, String delim, int index) {

		// return null if the delim token is not in the string
		if (str.indexOf(delim) == -1)
			return null;

		// loop the string and get the [index]'th substring
		int i = 0;
		StringTokenizer st = new StringTokenizer(str, delim);

		while (st.hasMoreElements()) {
			String s = (String) st.nextElement();
			if (i++ == index)
				return s;
		}

		// index out of bounds so return null
		return null;
	}

	/**
	 * Splits the string into a List of Strings using the delimeter. <br/>Example 1: <code>str = "abc;def;ghi;jkl", delim=";"</code> will return a list containing <br/><code>abc</code> <br/><code>def</code>
	 * <br/><code>ghi</code> <br/><code>jkl</code> <br/><br/>Example 2 (no substring between two delimiters): <code>str = "abc;def;;jkl", delim=";"</code> will return a list containing <br/><code>abc</code>
	 * <br/><code>def</code> <br/><code>jkl</code> <br/><br/>Will return null if the seperator is not in the string.
	 * 
	 * @param str
	 * string to seperate.
	 * @param delim
	 * delimiter/seperator.
	 * @return list containing the substrings.
	 */
	public static List splitString(String str, String delim) {

		// return null if the delim token is not in the string
		if (str.indexOf(delim) == -1)
			return null;

		List list = new ArrayList();

		// add substrings to the list
		StringTokenizer st = new StringTokenizer(str, delim);
		while (st.hasMoreElements()) {
			list.add(st.nextToken());
		}

		return list;
	}

	/**
	 * Use this method to seperate a string with seperator " ;" into substrings with empty strings. <br/>If there is no character between two seperators, the field will not be missing and will be set
	 * to "". <br/>If the string contains any semi-colon except for " ;", return null. <br/>if the string is ended with " ;", the last element of the return string array will be "". <br/>if the string
	 * contains no seperator, null is returned. <p/> Example 1: input string "1 ;2 ;3", will return {"1", "2", "3"} <br/>Example 2: input string "1 ; ;3 ;", will return {"1", "", "3"} <br/>Example 3:
	 * input string "1;2 ;3", will return null <br/>Example 4: input string "1 ;2 ;", will return {"1", "2", ""} <p/> The input string should not: <br/>- be null <br/>- or contain successive
	 * semi-comas <p/> Example 5: input string "1 ;;;2 ;3", returned will be {"1", "2", "3"}
	 * 
	 * @param data
	 * string to be seperated, should not be null
	 * @return seperated string array
	 */
	public static String[] seperate(String data) {
		// if no seperator then return null.
		if (data == null || data.indexOf(";") == -1)
			return null;

		// the input string should not be null
		StringTokenizer st = new StringTokenizer(data, ";");
		String[] strings;
		int i = 0;

		int count = st.countTokens();

		if (data.endsWith(";")) {
			// the string is ended with ";"
			strings = new String[count + 1];
			strings[count] = "";
		} else {
			// the string is not ended with ";"
			strings = new String[count];
			strings[count - 1] = data.substring(data.lastIndexOf(";") + 1);
			st = new StringTokenizer(data.substring(0, data.lastIndexOf(";")), ";");
		}

		while (st.hasMoreTokens()) {
			String temp = st.nextToken();
			if (!temp.endsWith(" ")) {
				// if any field between semi-comas is not ended with space, return null
				return null;
			}
			strings[i] = temp.substring(0, temp.length() - 1);
			i++;
		}

		return strings;
	}

	/**
	 * Trim the string. if the string is null an empty string is returned ("").
	 * 
	 * @param s
	 * the string.
	 * @return the trimmed string or empty.
	 */
	public static String trim(String s) {
		if (s == null)
			return "";
		else
			return s.trim();
	}

	/**
	 * Construts a list of string into a whole string sperated by comma. <p/>Example: if you put in <code>"aa"</code>, <code>"bb"</code>, <code>"cc"</code>, you will get
	 * <code>"aa,bb,cc"</code>.
	 * 
	 * @param elementString
	 * a list of String.
	 * @return a String assebled by a list of String.
	 */
	public static String assembleString(Collection elementString) {
		StringBuffer sb = null;
		if (elementString.size() == 0) {
			return "";
		} else {
			sb = new StringBuffer();
			Iterator it = elementString.iterator();
			if (it.hasNext())
				sb.append(it.next());
			while (it.hasNext()) {
				sb.append(",");
				sb.append(it.next());
			}
			return sb.toString();
		}
	}

	/**
	 * Fills the string with the [fill] char so the total length of the string is [length]. <p/>The char is added to the start of the string. If the strings length already is longer than [length] then
	 * the string is not chagned.
	 * 
	 * @param s
	 * the string.
	 * @param length
	 * the desired length.
	 * @param fill
	 * the fill char.
	 * @return the changed string.
	 */
	public static String postFillStr(String s, int length, char fill) {
		if (s.length() < length) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < length - s.length(); i++) {
				sb.append(fill);
			}
			sb.append(s);
			return sb.toString();
		} else
			return s;
	}

	/**
	 * Gets the string in the part between the [before] and [after] strings. If the string is not found, null is returned. Ignores the case of the strings.
	 * 
	 * @param str
	 * the string.
	 * @param before
	 * the before string.
	 * @param after
	 * the after string.
	 * @return the string between before and after.
	 */
	public static String getMiddleString(String str, String before, String after) {
		String s = str.toLowerCase();
		int i = s.indexOf(before.toLowerCase());
		int j = s.indexOf(after.toLowerCase());
		if (i != -1 && j != -1)
			return str.substring(i + before.length(), j);
		else
			return null;
	}

	/**
	 * Counts the tokens in the string.
	 * 
	 * @param str
	 * the string.
	 * @param delim
	 * the delim,
	 * @return number of tokens in the string.
	 */
	public static int countToken(String str, String delim) {
		StringTokenizer st = new StringTokenizer(str, delim);
		return st.countTokens();
	}

	/**
	 * Counts the delims in the string.
	 * 
	 * @param str
	 * the string.
	 * @param delim
	 * the delim,
	 * @return number of delims in the string.
	 */
	public static int countDelim(String str, String delim) {
		int tokens = countToken(str, delim);
		if (tokens > 1)
			return tokens - 1;
		else
			return 0;
	}

	/**
	 * Gets the [index] substring of the string.
	 * 
	 * @param str
	 * the string.
	 * @param delim
	 * the delimeter.
	 * @param index
	 * the substring at position [index].
	 * @return the substring or str if out of bounds.
	 */
	public static String getStringAt(String str, String delim, int index) {
		List list = splitString(str, delim);
		if (list != null && list.size() > index)
			return (String) list.get(index);
		else
			return str;
	}

	/**
	 * Returns the part of the string before the token (token not included) <p/> Returns the entire input string if the token is not found in the string. Ignores the case when searching for the token.
	 * 
	 * @param input
	 * the input string.
	 * @param token
	 * the token.
	 * @return
	 */
	public static String before(String input, String token) {
		int idx = input.toLowerCase().indexOf(token.toLowerCase());
		if (idx > 0)
			return input.substring(0, idx);
		else
			return input;
	}

	/**
	 * Returns the part of the string after the token (token not included) <p/> Returns the entire input string if the token is not found in the string. Ignores the case when searching for the token.
	 * 
	 * @param input
	 * the input string.
	 * @param token
	 * the token.
	 * @return
	 */
	public static String after(String input, String token) {
		int idx = input.toLowerCase().indexOf(token.toLowerCase());
		if (idx > 0)
			return input.substring(idx + token.length(), input.length());
		else
			return input;
	}

	/**
	 * Replaces the input string with the replace value at the [index] position. <p/> Returns the input string if the [index] is out of bounds.
	 * 
	 * @param input
	 * the input string.
	 * @param replace
	 * the value to replace.
	 * @param index
	 * start position to replace the text.
	 * @return the replaced text.
	 */
	public static String replaceString(String input, String replace, int index) {
		if (index > input.length())
			return input;

		StringBuffer sb = new StringBuffer(input.length());

		sb.append(input.substring(0, index));
		sb.append(replace);

		// should we append the rest
		if (index + replace.length() < input.length())
			sb.append(input.substring(index + replace.length(), input.length()));

		return sb.toString();
	}

	static public String ISO2GBK(String str) {
		try {
			str = new String(str.getBytes("8859_1"), "GB2312");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("Error converting string \'" + str + "\'", e);
		}
		return str;
	}

	/**
	 * If String is NULL, return "".
	 * 
	 * @param s
	 * @return
	 */
	public static String doNull(String s) {
		if (s == null) {
			return "";
		} else {
			return s;
		}
	}

	/**
	 * if i = NULL, return j, ortherwise return i.
	 * 
	 * @param i
	 * @param j
	 * @return
	 */
	public static String doNull(String i, String j) {
		if (i == null) {
			return j;
		} else {
			return i;
		}
	}

	/**
	 * if i != NULL or i != 0, return i, otherwise return j.
	 * 
	 * @param i

⌨️ 快捷键说明

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