public class StringUtils
extends java.lang.Object
Operations on String
that are
null
safe.
The StringUtils
class defines certain words related to
String handling.
null
""
)' '
, char 32)Character.isWhitespace(char)
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
.
String
限定符和类型 | 方法和说明 |
---|---|
static java.lang.String |
alwaysNotNull(java.lang.String txt)
生成一个非null的字符串
|
static boolean |
contains(java.lang.String text,
java.lang.String ch)
判断字符串text是否包含字符串ch
|
static java.lang.String |
cutStringEndWith(java.lang.String org,
java.lang.String mark)
1??
|
static java.lang.String |
cutStringStartWith(java.lang.String org,
java.lang.String mark)
处理已A字符开头的字符,去除掉A
|
static boolean |
equals(java.lang.String obj1,
java.lang.String obj2)
peter:比较相等.
|
static boolean |
equalsIgnore(java.lang.String str1,
java.lang.String str2,
java.lang.String attach)
richer:判断两个字符串是否出去前后的附加字符串外是相等的
eg:equalsIgnore("/File/", "File", "/") == true
不支持传null path
|
static boolean |
equalsIgnoreCase(java.lang.String s1,
java.lang.String s2)
不区分大小写比较字符串
|
static int |
getLength(java.lang.String str)
获取字符串的长度,如果是null则返回0
|
static boolean |
isArrayType(java.lang.String text)
是否是数组类型的字符串
|
static boolean |
isBlank(java.lang.String str)
检查一个字符串是否为空白字符串
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false
|
static boolean |
isEmpty(java.lang.String str)
检查一个字符串是否是空字符串
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("bob") = false
StringUtils.isEmpty(" bob ") = false
|
static boolean |
isNotBlank(java.lang.String str)
检查一个字符串是否不是空白字符串
StringUtils.isNotBlank(null) = false
StringUtils.isNotBlank("") = false
StringUtils.isNotBlank(" ") = false
StringUtils.isNotBlank("bob") = true
StringUtils.isNotBlank(" bob ") = true
|
static boolean |
isNotEmpty(java.lang.String str)
检查一个字符串是否不为空字符串
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
|
static java.lang.String |
join(java.lang.String seperator,
java.lang.String[] strings)
carl:拼接数组字符
|
static java.lang.String |
parseVersion(java.lang.String xmlDesignerVersion) |
static java.lang.String |
perfectEnd(java.lang.String str,
java.lang.String attach)
检查一个字符串是否以某个指定的字符串结束,如果是的话不做改变,如果不是的话将把指定的字符串添加到原
字符串的末尾位置
|
static java.lang.String |
perfectStart(java.lang.String str,
java.lang.String attach)
检查一个字符串是否以某个指定的字符串开始,如果是的话不做改变,如果不是的话将把指定的字符串添加到原
字符串的起始位置
|
static java.lang.String |
perfectSurround(java.lang.String str,
java.lang.String attach) |
static java.lang.String |
rightPad(java.lang.String str,
int size)
右侧填充空格直到字符串达到指定长度
|
static java.lang.String[][] |
stringToArray(java.lang.String v) |
static java.lang.String |
subStringByByteLength(java.lang.String originString,
java.lang.String charsetName,
int byteLength)
根据字节数截取字符串
|
static java.util.StringTokenizer |
text2StringTokenizer(java.lang.String text)
text 做兼容类.
|
static java.lang.String |
trim(java.lang.String str)
Removes control characters (char <= 32) from both
ends of this String, handling
null by returning
null . |
static java.lang.String |
trimToNull(java.lang.String str)
去掉字符串首尾的空白,如果剩余的结果是空白字符串那么返回null
|
public static final java.lang.String EMPTY
public static final java.lang.String BLANK
public static java.lang.String alwaysNotNull(java.lang.String txt)
txt
- 原对象public static boolean isEmpty(java.lang.String str)
检查一个字符串是否是空字符串
StringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false
str
- 被检查的字符串,可能为nullpublic static boolean isNotEmpty(java.lang.String str)
检查一个字符串是否不为空字符串
StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true
str
- 被检查的字符串,可能是nullpublic static boolean isBlank(java.lang.String str)
检查一个字符串是否为空白字符串
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
str
- 被检查的字符串public static java.lang.String cutStringStartWith(java.lang.String org, java.lang.String mark)
org
- 原字符mark
- 标记字符public static java.lang.String cutStringEndWith(java.lang.String org, java.lang.String mark)
org
- 全部字符mark
- 标记Bpublic static boolean isNotBlank(java.lang.String str)
检查一个字符串是否不是空白字符串
StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true
str
- 被检查的字符串public static java.lang.String trim(java.lang.String str)
Removes control characters (char <= 32) from both
ends of this String, handling null
by returning
null
.
The String is trimmed using String.trim()
.
Trim removes start and end characters <= 32.
To strip whitespace use #(String)
.
To trim your choice of characters, use the
#(String, String)
methods.
StringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"
str
- the String to be trimmed, may be nullnull
if null String inputpublic static java.lang.String trimToNull(java.lang.String str)
public static java.lang.String perfectStart(java.lang.String str, java.lang.String attach)
public static java.lang.String perfectEnd(java.lang.String str, java.lang.String attach)
public static java.lang.String perfectSurround(java.lang.String str, java.lang.String attach)
public static int getLength(java.lang.String str)
public static boolean equalsIgnore(java.lang.String str1, java.lang.String str2, java.lang.String attach)
public static boolean contains(java.lang.String text, java.lang.String ch)
text
- 主字符串ch
- 寻找的字符串public static java.util.StringTokenizer text2StringTokenizer(java.lang.String text)
text
- 文本兼容public static java.lang.String join(java.lang.String seperator, java.lang.String[] strings)
public static java.lang.String parseVersion(java.lang.String xmlDesignerVersion)
public static boolean isArrayType(java.lang.String text)
text
- 目标字符串public static java.lang.String[][] stringToArray(java.lang.String v)
public static java.lang.String subStringByByteLength(java.lang.String originString, java.lang.String charsetName, int byteLength) throws java.io.UnsupportedEncodingException
originString
- 原始字符串charsetName
- 字符编码byteLength
- 字节长度java.io.UnsupportedEncodingException
public static boolean equals(java.lang.String obj1, java.lang.String obj2)
obj1
- may be null.obj2
- may be null.public static boolean equalsIgnoreCase(java.lang.String s1, java.lang.String s2)
public static java.lang.String rightPad(java.lang.String str, int size)