JAVA StringUtils工具類
org.apache.commons.lang
Class StringUtils
java.lang.Object
org.apache.commons.lang.StringUtils
public class StringUtilsextends Object
Operations on String
that are null
safe.
- IsEmpty/IsBlank - checks if a String contains text
- Trim/Strip - removes leading and trailing whitespace
- Equals
- startsWith - check if a String starts with a prefix null-safe
- endsWith - check if a String ends with a suffix null-safe
- IndexOf/LastIndexOf/Contains - null-safe index-of checks
- IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
- ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
- Substring/Left/Right/Mid - null-safe substring extractions
- SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
- Split/Join - splits a String into an array of substrings and vice versa
- Remove/Delete - removes part of a String
- Replace/Overlay - Searches a String and replaces one String with another
- Chomp/Chop - removes the last part of a String
- LeftPad/RightPad/Center/Repeat - pads a String
- UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
- CountMatches - counts the number of occurrences of one String in another
- IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
- DefaultString - protects against a null input String
- Reverse/ReverseDelimited - reverses a String
- Abbreviate - abbreviates a string using ellipsis
- Difference - compares Strings and reports on their differences
- LevensteinDistance - the number of changes needed to change one String into another
The StringUtils
class defines certain words related to String handling.
- null -
null
- empty - a zero-length string (
""
) - space - the space character (
‘ ‘
, char 32) - whitespace - the characters defined by
Character.isWhitespace(char)
- trim - the characters <= 32 as in
String.trim()
StringUtils
handles null
input Strings quietly. That is to say that a null
input will return null
. Where a boolean
or int
is being returned details vary by method.
A side effect of the null
handling is that a NullPointerException
should be considered a bug in StringUtils
(except for deprecated methods).
Methods in this class give sample code to explain their operation. The symbol *
is used to indicate any input including null
.
翻譯:Google
org.apache.commons.lang
類StringUtils
java.lang.Object
org.apache.commons.lang.StringUtils
公共類StringUtils擴展Object
這樣做的操作String
是 null
安全的。
- IsEmpty / IsBlank - 檢查一個String是否包含文本
- 修剪/去除 - 去除前導和尾隨的空白
- 等於 - 比較兩個字符串無效
- startsWith - 檢查一個字符串是否以一個前綴為null開頭
- endsWith - 檢查一個String是否以一個後綴為null結尾
- IndexOf / LastIndexOf / Contains - 無效索引的檢查
- IndexOfAny / LastIndexOfAny / IndexOfAnyBut / LastIndexOfAnyBut - 索引 - 任何一組字符串
- ContainsOnly / ContainsNone / ContainsAny - 是String只包含/ none /任何這些字符
- 子字符串/左/右/中 - 零安全子串提取
- SubstringBefore / SubstringAfter / SubstringBetween - 相對於其他字符串的子字符串提取
- 拆分/連接 - 將一個字符串拆分為一個子字符串數組,反之亦然
- 刪除/刪除 - 刪除部分字符串
- 替換/覆蓋 - 搜索一個字符串並用另一個替換一個字符串
- Chomp / Chop - 刪除字符串的最後部分
- 左鍵盤/右鍵盤/中心/重復 - 填充字符串
- UpperCase / LowerCase / SwapCase / Capitalize / Uncapitalize - 更改字符串的大小寫
- CountMatches - 統計另一個字符串的出現次數
- IsAlpha / IsNumeric / IsWhitespace / IsAsciiPrintable - 檢查字符串中的字符
- DefaultString - 防止空輸入字符串
- Reverse / ReverseDelimited - 反轉字符串
- 縮寫 - 使用省略號縮寫字符串
- 差異 - 比較字符串和報告差異
- LevensteinDistance - 將一個字符串更改為另一個字符串所需的更改次數
在StringUtils
類定義與字符串處理某些詞。
- 空值 -
null
- 空 - 一個零長度的字符串(
""
) - 空格 - 空格字符(
‘ ‘
char 32) - 空格 - 由...定義的字符
Character.isWhitespace(char)
- 修剪 - 在<= 32的字符
String.trim()
StringUtils
null
靜靜地處理輸入字符串。這就是說,一個null
輸入將返回null
。在哪裏boolean
或int
正在退貨的細節因方法而異。
處理的一個副作用null
是a NullPointerException
應該被認為是一個錯誤 StringUtils
(除了被棄用的方法)。
這個類中的方法給出了示例代碼來解釋它們的操作。該符號*
用於指示包括的任何輸入null
。
檢查字符串是否為空
isNotEmpty 將空格也作為參數,isNotBlank 則排除空格參數
isNoneEmpty 可添加多個參數將空格也作為參數 , isNoneBlank 可添加多個參數,排除空格參數
去掉字符串前後的空白
Trim/Strip
比較兩個字符串是否相等
Equals
檢查字符串是否以null前綴為開頭
startsWith
檢查字符串是否以null後綴為結尾
檢查字符串是否包含一個特定的字符
IndexOf / LastIndexOf / Contains
JAVA StringUtils工具類