📄 stringutil.java
字号:
/**
* 字符串处理方法大全
* Created on 2005-7-19
* @author WuQiaoYun
*/
package com.common.util;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import com.emk.manage.Constants;
import com.emk.manage.exception.EncodingException;
import com.emk.manage.exception.IllegalArgumentOfException;
/**
* 字符串处理方法大全
* 主要方法包括:
* <ul>
* <li>unicodeToGB - 由"GBK"码转换为"ISO8859_1"码
* <li>GBToUnicode - 由"ISO8859_1"码转换为"GBK"码
* <li>replace - 在字符串中进行查找替换,给出替换后的字符串(update)
* <li>split - 使用给定的分隔符拆分字符串成字符串数组(new);
* <li>join - 组合数据结构中所有元素成一个字符串(new);
* <li>repeat - 重复多次组合成新的字符串(new);
* <li>is... - 检查字符串中包含的字符的类型(new);
* <li>defaultString - 返回默认的字符串,将null变为空白字符(new);
* <li>htmlEncode - 在字符串中替换html标记所用的六个特殊字符:& \ " ' < >
* <li>return2Br - 将字符串中的换行符"\n"转换为html换行标记"<br>"
* <li>blank2NBSP - 将字符串中的空格" "转换为html的空格标记"&nbsp;"
* <li>urlEncodeNPath - 能解决一些路径中的文件名包含中文的问题
* <li>cutShort - 使用省略号(...)将过长的字符串截短
* <li>htmlCutShort - 使用省略号(...)将过长的字符串截短并转码html中的特殊字符
* <li>escapeHtml - 使用HTML entities中出现的字符,将字符串中的需要转换的字符全部逃逸
* <li>unescapeHtml - escapeHtml的反操作
* <li>escapeXml - 使用XML entities中出现四个常用的字符,将字符串中的需要转换的字符全部逃逸
* <li>unescapeXml - escapeXml的反操作
* <li>toToken - 除去字符串中的所有逗号分割符
* <li>toToken - 除去字符串中的所有指定的分割符
* <li>leftTrim - 将字符串左边的所有空格去掉。
* <li>rightTrim - 将字符串右边的所有空格去掉。
* <li>leftTrim - 将字符串左边的所有空格去掉。
* <li>reverseString - 反转字符串。
* </ul>
*/
public class StringUtil {
public StringUtil() {}
/**
* 转换中文字符集,由"GBK"码转换为"ISO8859_1"码。
*
* @param sSource 需要转换的字符串。
* @return 返回转换后的字符串。
*/
public static String unicodeToGB(String sSource) throws EncodingException {
try {
String S_Desc = "";
if (sSource != null && !sSource.trim().equals("")) {
byte b_Proc [] = sSource.getBytes("GBK");
S_Desc = new String(b_Proc, "ISO8859_1");
}
return S_Desc;
}catch(UnsupportedEncodingException e) {
EncodingException encode = new EncodingException(e.getMessage());
encode.setMessageFileName(Constants.MESSAGE_FILENAME);
encode.setMessageCode("resource_StringUtil_002");
encode.setExtandMessage("GBK");
throw encode;
}
}
/**
* 转换中文字符集,由"ISO8859_1"码转换为"GBK"码。
*
* @param sSource 需要转换的字符串。
* @return 返回转换后的字符串。
*/
public static String GBToUnicode(String sSource) throws EncodingException {
try {
String S_Desc = "";
if (sSource != null && !sSource.trim().equals("")) {
byte b_Proc [] = sSource.getBytes("ISO8859_1");
S_Desc = new String(b_Proc, "GBK");
}
return S_Desc;
}catch(UnsupportedEncodingException e) {
EncodingException encode = new EncodingException(e.getMessage());
encode.setMessageCode("resource_StringUtil_002");
encode.setExtandMessage("GBK");
throw encode;
}
}
/**
* 在字符串中进行查找替换,给出替换后的字符串.
*
* 如果是null 传入将返回不替换而返回原字符串.
* 例子:
* <pre>
* StringUtil.replace(null, *, *) = null
* StringUtil.replace("", *, *) = ""
* StringUtil.replace("aba", null, null) = "aba"
* StringUtil.replace("aba", null, null) = "aba"
* StringUtil.replace("aba", "a", null) = "aba"
* StringUtil.replace("aba", "a", "") = "aba"
* StringUtil.replace("aba", "a", "z") = "zbz"
* </pre>
*
* @param text text to search and replace in, may be null
* @param repl the String to search for, may be null
* @param with the String to replace with, may be null
* @return the text with any replacements processed,
* <code>null</code> if null String input
*/
public static String replace(String text, String repl, String with) {
return StringUtils.replace(text,repl,with);
}
/**
* 在字符串中替换html标记所用的六个特殊字符:& \ " ' < >
*
* @param sSource 要替换的字符串。
* @return 返回替换后的字符串。
*/
public static String htmlEncode(String sSource) {
String sTemp = sSource;
sTemp = replace(sTemp, "&", "&");
sTemp = replace(sTemp, "\"", """);
sTemp = replace(sTemp, "'", "'");
sTemp = replace(sTemp, "<", "<");
sTemp = replace(sTemp, ">", ">");
return sTemp;
}
/**
* 将字符串中的换行符"\n"转换为html换行标记"<br>"
*
* @param sSource 要替换的字符串。
* @return 返回替换后的字符串。
*/
public static String return2Br(String sSource) {
return replace(sSource, "\n", "<br>");
}
/**
* 将字符串中的空格" "转换为html的空格标记"&nbsp;"
*
* @param sSource 要替换的字符串。
* @return 返回替换后的字符串。
*/
public static String blank2NBSP(String sSource) {
return replace(sSource, " ", " ");
}
/**
* 使用给定的分隔符将传入的字符串分割成字符串数组.
*
* 分隔符不包括在返回的字符串数组中.相邻的分隔符作为一个分隔符处理
*
* A <code>null</code> input String returns <code>null</code>.
* 例子:
* <pre>
* StringUtil.split(null, *) = null
* StringUtil.split("", *) = []
* StringUtil.split("a.b.c", '.') = ["a", "b", "c"]
* StringUtil.split("a..b.c", '.') = ["a", "b", "c"]
* StringUtil.split("a:b:c", '.') = ["a:b:c"]
* StringUtil.split("a\tb\nc", null) = ["a", "b", "c"]
* StringUtil.split("a b c", ' ') = ["a", "b", "c"]
* </pre>
*
* @param str the String to parse, may be null
* @param separatorChar the character used as the delimiter,
* <code>null</code> splits on whitespace
* @return an array of parsed Strings, <code>null</code> if null String input
*/
public static String[] split(String str, char separatorChar) {
return StringUtils.split(str,separatorChar);
}
/**
* 使用给定的分隔符将传入的字符串分割成字符串数组.
* 分隔符不包括在返回的字符串数组中.相邻的分隔符作为一个分隔符处理
*
* <p>A <code>null</code> input String returns <code>null</code>.
* A <code>null</code> separatorChars splits on whitespace.</p>
* 例子:
* <pre>
* StringUtil.split(null, *) = null
* StringUtil.split("", *) = []
* StringUtil.split("abc def", null) = ["abc", "def"]
* StringUtil.split("abc def", " ") = ["abc", "def"]
* StringUtil.split("abc def", " ") = ["abc", "def"]
* StringUtil.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
* </pre>
*
* @param str the String to parse, may be null
* @param separatorChars the characters used as the delimiters,
* <code>null</code> splits on whitespace
* @return an array of parsed Strings, <code>null</code> if null String input
*/
public static String[] split(String str, String separatorChars) {
return StringUtils.split(str, separatorChars);
}
/**
* 把一个传入的字符串重复 <code>repeat</code> 次,然后组成一个新的字符串.
* 例子:
* <pre>
* StringUtil.repeat(null, 2) = null
* StringUtil.repeat("", 0) = ""
* StringUtil.repeat("", 2) = ""
* StringUtil.repeat("a", 3) = "aaa"
* StringUtil.repeat("ab", 2) = "abab"
* StringUtil.repeat("a", -2) = ""
* </pre>
*
* @param str the String to repeat, may be null
* @param repeat number of times to repeat str, negative treated as zero
* @return a new String consisting of the original String repeated,
* <code>null</code> if null String input
*/
public static String repeat(String str, int repeat) {
return StringUtils.repeat(str,repeat);
}
/**
* <p>将提供的数组中的元素组合成一个字符串.</p>
*
* <p>No separator is added to the joined String.
* Null objects or empty strings within the array are represented by
* empty strings.</p>
* 例子:
* <pre>
* StringUtil.join(null) = null
* StringUtil.join([]) = ""
* StringUtil.join([null]) = ""
* StringUtil.join(["a", "b", "c"]) = "abc"
* StringUtil.join([null, "", "a"]) = "a"
* </pre>
*
* @param array the array of values to join together, may be null
* @return the joined String, <code>null</code> if null array input
*/
public static String join(Object[] array) {
return StringUtils.join(array);
}
/**
* <p>将提供的数组中的元素组合成一个字符串.</p>
*
* <p>No delimiter is added before or after the list.
* Null objects or empty strings within the array are represented by
* empty strings.</p>
* 例子:
* <pre>
* StringUtil.join(null, *) = null
* StringUtil.join([], *) = ""
* StringUtil.join([null], *) = ""
* StringUtil.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtil.join(["a", "b", "c"], null) = "abc"
* StringUtil.join([null, "", "a"], ';') = ";;a"
* </pre>
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, <code>null</code> if null array input
*/
public static String join(Object[] array, char separator) {
return StringUtils.join(array,separator);
}
/**
* <p>将提供的数组中的元素组合成一个字符串.</p>
*
* <p>No delimiter is added before or after the list.
* A <code>null</code> separator is the same as an empty String ("").
* Null objects or empty strings within the array are represented by
* empty strings.</p>
* 例子:
* <pre>
* StringUtil.join(null, *) = null
* StringUtil.join([], *) = ""
* StringUtil.join([null], *) = ""
* StringUtil.join(["a", "b", "c"], "--") = "a--b--c"
* StringUtil.join(["a", "b", "c"], null) = "abc"
* StringUtil.join(["a", "b", "c"], "") = "abc"
* StringUtil.join([null, "", "a"], ',') = ",,a"
* </pre>
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use, null treated as ""
* @return the joined String, <code>null</code> if null array input
*/
public static String join(Object[] array, String separator) {
return StringUtils.join(array,separator);
}
/**
* <p>将提供的 <code>Iterator</code>中的元素组合成一个字符串.</p>
*
* <p>No delimiter is added before or after the list. Null objects or empty
* strings within the iteration are represented by empty strings.</p>
*
* <p>See the examples here: {@link #join(Object[],char)}. </p>
*
* @param iterator the <code>Iterator</code> of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, <code>null</code> if null iterator input
*/
public static String join(Iterator iterator, char separator) {
return StringUtils.join(iterator,separator);
}
/**
* <p>将提供的 <code>Iterator</code>中的元素组合成一个字符串.</p>
*
* <p>No delimiter is added before or after the list.
* A <code>null</code> separator is the same as an empty String ("").</p>
*
* <p>See the examples here: {@link #join(Object[],String)}. </p>
*
* @param iterator the <code>Iterator</code> of values to join together, may be null
* @param separator the separator character to use, null treated as ""
* @return the joined String, <code>null</code> if null iterator input
*/
public static String join(Iterator iterator, String separator) {
return StringUtils.join(iterator,separator);
}
/**
* <p>检查是否字符串仅包含字母.</p>
*
* <p><code>null</code> will return <code>false</code>.
* An empty String ("") will return <code>true</code>.</p>
* 例子:
* <pre>
* StringUtil.isAlpha(null) = false
* StringUtil.isAlpha("") = true
* StringUtil.isAlpha(" ") = false
* StringUtil.isAlpha("abc") = true
* StringUtil.isAlpha("ab2c") = false
* StringUtil.isAlpha("ab-c") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if only contains letters, and is non-null
*/
public static boolean isAlpha(String str) {
return StringUtils.isAlpha(str);
}
/**
* <p>检查是否字符串仅包含字母或数字.</p>
*
* <p><code>null</code> will return <code>false</code>.
* An empty String ("") will return <code>true</code>.</p>
* 例子:
* <pre>
* StringUtil.isAlphanumeric(null) = false
* StringUtil.isAlphanumeric("") = true
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -