📄 stringutil.java
字号:
* StringUtil.isAlphanumeric(" ") = false
* StringUtil.isAlphanumeric("abc") = true
* StringUtil.isAlphanumeric("ab c") = false
* StringUtil.isAlphanumeric("ab2c") = true
* StringUtil.isAlphanumeric("ab-c") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if only contains letters or digits,
* and is non-null
*/
public static boolean isAlphanumeric(String str) {
return StringUtils.isAlphanumeric(str);
}
/**
* <p>检查是否字符串仅包含数字,浮点数不是一个数字,所以返回false.</p>
*
* <p><code>null</code> will return <code>false</code>.
* An empty String ("") will return <code>true</code>.</p>
* 例子:
* <pre>
* StringUtil.isNumeric(null) = false
* StringUtil.isNumeric("") = true
* StringUtil.isNumeric(" ") = false
* StringUtil.isNumeric("123") = true
* StringUtil.isNumeric("12 3") = false
* StringUtil.isNumeric("ab2c") = false
* StringUtil.isNumeric("12-3") = false
* StringUtil.isNumeric("12.3") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if only contains digits, and is non-null
*/
public static boolean isNumeric(String str) {
return StringUtils.isNumeric(str);
}
/**
* <p>检查是否字符串仅包含空白.</p>
*
* <p><code>null</code> will return <code>false</code>.
* An empty String ("") will return <code>true</code>.</p>
* 例子:
* <pre>
* StringUtil.isWhitespace(null) = false
* StringUtil.isWhitespace("") = true
* StringUtil.isWhitespace(" ") = true
* StringUtil.isWhitespace("abc") = false
* StringUtil.isWhitespace("ab2c") = false
* StringUtil.isWhitespace("ab-c") = false
* </pre>
*
* @param str the String to check, may be null
* @return <code>true</code> if only contains whitespace, and is non-null
*/
public static boolean isWhitespace(String str) {
return StringUtils.isWhitespace(str);
}
/**
* <p>返回原字符串,当传入的字符串是<code>null</code>时, 返回空字符串("").</p>
* 例子:
* <pre>
* StringUtil.defaultString(null) = ""
* StringUtil.defaultString("") = ""
* StringUtil.defaultString("bat") = "bat"
* </pre>
*
* @param str the String to check, may be null
* @return the passed in String, or the empty String if it
* was <code>null</code>
*/
public static String defaultString(String str) {
return StringUtils.defaultString(str);
}
/**
* <p>返回原字符串,当传入的字符串是<code>null</code>或者为空字符串时, 返回输入的默认值.</p>
* 例子:
* <pre>
* StringUtil.defaultString(null, "null") = "null"
* StringUtil.defaultString("", "null") = "null"
* StringUtil.defaultString("bat", "null") = "bat"
* </pre>
*
* @param str the String to check, may be null
* @param defaultStr the default String to return
* if the input is <code>null</code>, may be null
* @return the passed in String, or the default if it was <code>null</code> or empty string.
*/
public static String defaultString(String str, String defaultStr) {
if(str==null || str.equals("")){
return defaultStr;
}else {
return str;
}
}
/**
*将路径中的文件名单独取出并用URLEncode编码然后组成新的路径,能解决一些路径中的文件名包含中文的问题
*
*@param path 路径字符串
*@return 返回编码后的路径字符串,如果参数为空或者长度为0返回0长度字符串
*/
public static String urlEncodeNPath(String path) {
String fileName = "", prefix = "", suffix = "";
if (path == null || path.trim().length() == 0)
return "";
int intDot = path.lastIndexOf(".");
int intSlash = path.lastIndexOf("/");
//取最靠后的正斜杠或者反斜杠
intSlash = (intSlash >= path.lastIndexOf("\\")) ? intSlash : path.lastIndexOf("\\");
if (intSlash == -1)
intSlash = 0;
else
intSlash = intSlash + 1;
if (intDot > intSlash)
suffix = path.substring(intDot, path.length());
//System.out.println("suffix: " + suffix);
prefix = path.substring(0, intSlash);
//System.out.println("prefix: " + prefix);
if (intDot > intSlash)
fileName = path.substring(intSlash, intDot);
else
fileName = path.substring(intSlash, path.length());
//System.out.println("filename: " + fileName);
String temp="";
try {
temp=prefix + URLEncoder.encode(fileName,"GBK") + suffix;
} catch (Exception e) {}
return temp;
}
/**
* 使用省略号(...)将过长的字符串截短。
*
* 使用注意:
* 如果str的长度短于maxWidth个字符个数,返回str本身.
* 如果str的长度长于maxWidth个字符个数,将被截短到(substring(str, 0, max-3) + "...")
* 如果maxWidth小于4, 异常IllegalArgumentException将会抛出.
* 返回的字符串的长度永远不会长于maxWidth
*
* <pre>
* StringUtil.cutShort(null, *) = null
* StringUtil.cutShort("", 4) = ""
* StringUtil.cutShort("abcdefg", 6) = "abc..."
* StringUtil.cutShort("abcdefg", 7) = "abcdefg"
* StringUtil.cutShort("abcdefg", 8) = "abcdefg"
* StringUtil.cutShort("abcdefg", 4) = "a..."
* StringUtil.cutShort("abcdefg", 3) = IllegalArgumentException
* </pre>
*
* @param str 要被截短的字符串,可以为 null
* @param maxWidth 截短后字符串的最大长度, 但必须大于等于 4
* @return 截短后的字符串, 如果传入null字符串则返回<code>null</code>
* @throws IllegalArgumentException 如果长度小于 4
*/
public static String cutShort(String str, int maxWidth)throws IllegalArgumentOfException {
try{
return StringUtils.abbreviate(str, 0, maxWidth);
}catch(IllegalArgumentException e) {
IllegalArgumentOfException ille = new IllegalArgumentOfException(e.getMessage());
ille.setMessageCode("resource_StringUtil_001");
ille.setMessageFileName(Constants.MESSAGE_FILENAME);
throw ille;
}
}
/**
* 使用省略号(...)将过长的字符串截短并转码html中的特殊字符
* @param str 要被截短的字符串,可以为 null
* @param maxWidth 截短后字符串的最大长度, 但必须大于等于 4
* @return 截短后并转码了html中的特殊字符的字符串, 如果传入null字符串则返回<code>null</code>
* @throws IllegalArgumentException 如果长度小于 4
*/
public static String htmlCutShort(String str, int maxWidth)throws IllegalArgumentOfException{
try {
return htmlEncode(cutShort(str, maxWidth));
}catch(IllegalArgumentException e) {
IllegalArgumentOfException ille = new IllegalArgumentOfException(e.getMessage());
ille.setMessageFileName(Constants.MESSAGE_FILENAME);
ille.setMessageCode("resource_StringUtil_001");
throw ille;
}
}
/**
* 使用HTML entities中出现的字符,将字符串中的需要转换的字符全部逃逸.
* 例如: <tt>"bread" & "butter"</tt> => <tt>&quot;bread&quot; &amp; &quot;butter&quot;</tt>.
*
* <p>支持所有的已知的 HTML 4.0 entities.</p>
*
* @param str 需要进行HTML逃逸字符出来的字符串,可能为 null
* @return 返回已经进行逃逸处理后的字符串, 如果输入参数为null字符串则返回null
*
* @see #unescapeHtml(String)
* @see </br><a href="http://hotwired.lycos.com/webmonkey/reference/special_characters/">ISO Entities</a>
* @see </br><a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
* @see </br><a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
* @see </br><a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
* @see </br><a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
**/
public static String escapeHtml(String str) {
return StringEscapeUtils.escapeHtml(str);
}
/**
* 反逃逸操作(解码).
*例如, 字符串 "&lt;Fran&ccedil;ais&gt;"
* 操作后将返回 "<Français>"
*如果entity不被识别,将逐字返回到结果字符串. 例:"&gt;&zzzz;x" 将变为
* ">&zzzz;x".
*
* @param str 将被反逃逸的字符串, 可能为 null
* @return 返回已经进行反逃逸处理后的字符串, 如果输入参数为null字符串则返回null
* @see #escapeHtml(String)
**/
public static String unescapeHtml(String str) {
return StringEscapeUtils.unescapeHtml(str);
}
/**
* 使用XML entities中出现四个常用的字符,将字符串中的需要转换的字符全部逃逸.
*
* <p>例如: <tt>"bread" & "butter"</tt> =>
* <tt>&quot;bread&quot; &amp; &quot;butter&quot;</tt>.
* </p>
*
* <p>仅支持逃逸这四个基本的 XML entities (gt, lt, quot, amp).
* 不支持 DTDs 或 扩展的实体(entities).</p>
*
* @param str 需要进行逃逸操作的字符串, 可能为 null
* @return 返回已经进行逃逸处理后的字符串, 如果输入参数为null字符串则返回null
* @see #unescapeXml(java.lang.String)
**/
public static String escapeXml(String str) {
return StringEscapeUtils.escapeXml(str);
}
/**
* 将字符串中包含 XML entity 的部分转换成实际的相应的Unicode字符.
* 仅支持逃逸这四个基本的 XML entities (gt, lt, quot, amp).
* 不支持 DTDs 或 扩展的实体(entities).
*
* @param str 需要进行反逃逸操作的字符串, 可能为 null
* @return 返回已经进行反逃逸处理后的字符串, 如果输入参数为null字符串则返回null
* @see #escapeXml(String)
**/
public static String unescapeXml(String str) {
return StringEscapeUtils.unescapeXml(str);
}
/**
* 把字符串中html标记所用的六个特殊字符替换回正常字符:&quot;(") &#039;(')
* &lt;(<) &gt;(>) &amp;(&) 。
*
* @param sSource 要替换的字符串。
* @return 返回替换后的字符串。
*/
public static String htmlDecode(String sSource) {
String sTemp = sSource;
sTemp = replace(sTemp, """, "\"");
sTemp = replace(sTemp, "'", "'");
sTemp = replace(sTemp, "<", "<");
sTemp = replace(sTemp, ">", ">");
sTemp = replace(sTemp, "&", "&");
return sTemp;
}
/**
* 除去字符串中的所有逗号分割符(,)
*
* @param s 需要处理的字符串
* @return 不含有逗号的字符串
*/
public static String toToken(String s) {
if (s==null || s.trim().equals(""))
return s;
String newStr = new String("");
StringTokenizer st = new StringTokenizer(s, ",");
while (st.hasMoreTokens()) {
newStr = newStr + st.nextToken();
}
return newStr;
}
/**
* 除去字符串中的所有指定的分割符
*
* @param s 需要处理的字符串
* @return 不含有逗号的字符串
*/
public static String toToken(String s,String val) {
if (s==null || s.trim().equals(""))
return s;
if (val==null || val.equals(""))
return s;
String newStr = new String("");
StringTokenizer st = new StringTokenizer(s, val);
while (st.hasMoreTokens()) {
newStr = newStr + st.nextToken();
}
return newStr;
}
/**
* 将字符串左边的所有空格去掉。
*
* @param str:将要被处理的字符串
* @return 左边没有空格的字符串
*/
public static String leftTrim(String str){
if (str==null || str.equals("")) return str;
StringBuffer sbf = new StringBuffer(str);
while (sbf.charAt(0)==' ') {
sbf = sbf.deleteCharAt(0);
}
return sbf.toString();
}
/**
* 将字符串右边的所有空格去掉。
* @param str:将要被处理的字符串
* @return 右边没有空格的字符串
*/
public static String rightTrim(String str){
if (str==null || str.equals("")) return str;
StringBuffer sbf = new StringBuffer(str);
while (sbf.charAt(sbf.length()-1)==' ') {
sbf = sbf.deleteCharAt(sbf.length()-1);
}
return sbf.toString();
}
/**
* 将字符串反转。
* @param str: 将要反转的字符串。
* @return 返回经过反转处理的字符串。
*/
public static String reverseString(String str){
if (str==null || str.equals("")) return str;
StringBuffer sbf = new StringBuffer(str);
return sbf.reverse().toString();
}
/**
* -----------------------------------
* @author 胡勇
* -----------------------------------
* 判断字符参数是否为空值
*/
public static boolean isNullValue(String value) {
return value == null || value.trim().equals("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -