public class StringUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
EMPTY_STRING |
| 构造器和说明 |
|---|
StringUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
capitalize(String str)
将字符串的第一个字符(必须在Character.toUpperCase(char)中,否则就不会改变)转换为大写.
|
static String |
changeCharacterCase(String str,
int index,
boolean capital)
将字符串中指定位置(
index)的字符(必须在Character.toLowerCase(char))转为大(小)写,否则就不会改变
StringUtils.changeCharacterCase("Hello",2,true); //--- "HeLlo"
|
static boolean |
containsWhitespace(CharSequence str)
判断字符串中是否存在空白符,方法会检查字符串是否为
null
StringUtils.containsWhitespace(null); //--- false
StringUtils.containsWhitespace(""); //--- false
StringUtils.containsWhitespace(" "); //--- true
StringUtils.containsWhitespace("ax x"); //--- true
|
static boolean |
containsWhitespace(String str)
判断字符串中是否存在空白符,方法会检查字符串是否为
null |
static String |
delete(String str,
String pattern)
将字符串
str中的出现了指定子串pattern全部删除,删除的字符串不支持正则表达式. |
static boolean |
endsWithIgnoreCase(String str,
String suffix)
判断字符串中是否以给定的字符串结尾,不考虑大小写问题.
|
static boolean |
hasLength(CharSequence str)
判断字符串是否为既不为
null,字符串长度也不为0.当传入参数是一个空白符时也返回true
StringUtils.hasLength(null);// --- false
StringUtils.hasLength("");// --- false
StringUtils.hasLength(" ");// --- true
StringUtils.hasLength("Hi");// --- true
|
static boolean |
hasLength(String str)
判断字符串是否为既不为
null,字符串长度也不为0.当传入参数是一个空白符时也返回true |
static boolean |
hasText(CharSequence str)
判断字符串是否存在文本字符,即字符只是存在一个非空白字符。
|
static boolean |
hasText(String str)
判断字符串是否存在文本字符,即字符只是存在一个非空白字符。
|
static boolean |
isEmpty(CharSequence str)
判断字符串是否为
null或空字符串,判断字符串是否为空请使用该方法. |
static boolean |
isEmpty(String str)
判断字符串是否为
null或空字符串,判断字符串是否为空请使用该方法. |
static String |
replace(String str,
String oldPattern,
String newPattern)
将字符串中所有出现
oldPattern替换为newPattern. |
static String[] |
split(String toSplit)
字符串分割,默认采用了英文半角逗号分割
|
static String[] |
split(String toSplit,
String delimiter)
字符串分割
|
static String[] |
splitAtFirst(String toSplit,
String delimiter)
从识别的第一处分割字符串
|
static boolean |
startsWithIgnoreCase(String str,
String prefix)
判断字符串中是否以给定的字符串开头,不考虑大小写问题
StringUtils.startsWithIgnoreCase("abcd",null); //--- false
StringUtils.startsWithIgnoreCase("abcd","ab"); //--- true
StringUtils.startsWithIgnoreCase("abcd","bc"); //--- false
StringUtils.startsWithIgnoreCase("abcd","AB"); //--- true
StringUtils.startsWithIgnoreCase(null,"ab"); //--- false
|
static String |
toCamelCase(String name)
将下划线大写方式命名的字符串转换为驼峰式。
|
static String |
toUnderline(String name)
将驼峰式命名的字符串转换为下划线字符串。
|
static String |
toUnderline(String name,
boolean upper)
将驼峰式命名的字符串转换为下划线字符串。
|
static String |
trim(String str)
去除字符串左右两侧的空白符
StringUtils.trim(null); //--- null
StringUtils.trim(""); //--- ""
StringUtils.trim(" "); //--- ""
StringUtils.trim(" a b "); //--- a b
|
static String |
trimLeftWhiteSpace(String str)
去除字符串左侧的空白字符
StringUtils.trimLeftWhiteSpace(null); //--- null
StringUtils.trimLeftWhiteSpace("ab"); //--- null
StringUtils.trimLeftWhiteSpace((" ab c");//--- ab c
|
static String |
trimRightWhiteSpace(String str)
去除字符串右侧的空白字符
StringUtils.trimRightWhiteSpace(null); //--- null
StringUtils.trimRightWhiteSpace("ab"); //--- null
StringUtils.trimRightWhiteSpace((" ab c ");//--- ab c
|
static String |
unCapitalize(String str)
将字符串的第一个字符(必须在Character.toLowerCase(char)中,否则就不会改变)转换为小写.
|
public static boolean isEmpty(CharSequence str)
null或空字符串,判断字符串是否为空请使用该方法.str - 字符串true,否则返回falsepublic static boolean isEmpty(String str)
null或空字符串,判断字符串是否为空请使用该方法.str - 字符串true,否则返回falsepublic static boolean hasLength(CharSequence str)
判断字符串是否为既不为null,字符串长度也不为0.当传入参数是一个空白符时也返回true
StringUtils.hasLength(null);// --- false
StringUtils.hasLength("");// --- false
StringUtils.hasLength(" ");// --- true
StringUtils.hasLength("Hi");// --- true
str - 字符串null,长度也不为0时就返回true,反之则返回false.public static boolean hasLength(String str)
null,字符串长度也不为0.当传入参数是一个空白符时也返回truestr - 字符串null,长度也不为0时就返回true,反之则返回false.hasLength(CharSequence)public static boolean hasText(CharSequence str)
判断字符串是否存在文本字符,即字符只是存在一个非空白字符。
StringUtils.hasText(null); //--- false
StringUtils.hasText(""); //--- false
StringUtils.hasText(" "); //--- false
StringUtils.hasText(" abc"); //--- true
StringUtils.hasText("a"); //--- true
str - 字符串true,否则返回falseCharacter.isWhitespace(char)public static boolean hasText(String str)
str - 字符串true,否则返回falsehasText(CharSequence)public static boolean containsWhitespace(CharSequence str)
判断字符串中是否存在空白符,方法会检查字符串是否为null
StringUtils.containsWhitespace(null); //--- false
StringUtils.containsWhitespace(""); //--- false
StringUtils.containsWhitespace(" "); //--- true
StringUtils.containsWhitespace("ax x"); //--- true
str - 字符串true,否则返回falseCharacter.isWhitespace(char)public static boolean containsWhitespace(String str)
nullstr - 字符串true,否则返回falsecontainsWhitespace(CharSequence)public static String trim(String str)
去除字符串左右两侧的空白符
StringUtils.trim(null); //--- null
StringUtils.trim(""); //--- ""
StringUtils.trim(" "); //--- ""
StringUtils.trim(" a b "); //--- a b
str - 字符串Character.isWhitespace(char)public static String trimLeftWhiteSpace(String str)
去除字符串左侧的空白字符
StringUtils.trimLeftWhiteSpace(null); //--- null
StringUtils.trimLeftWhiteSpace("ab"); //--- null
StringUtils.trimLeftWhiteSpace((" ab c");//--- ab c
str - 字符串public static String trimRightWhiteSpace(String str)
去除字符串右侧的空白字符
StringUtils.trimRightWhiteSpace(null); //--- null
StringUtils.trimRightWhiteSpace("ab"); //--- null
StringUtils.trimRightWhiteSpace((" ab c ");//--- ab c
str - 字符串public static boolean startsWithIgnoreCase(String str, String prefix)
判断字符串中是否以给定的字符串开头,不考虑大小写问题
StringUtils.startsWithIgnoreCase("abcd",null); //--- false
StringUtils.startsWithIgnoreCase("abcd","ab"); //--- true
StringUtils.startsWithIgnoreCase("abcd","bc"); //--- false
StringUtils.startsWithIgnoreCase("abcd","AB"); //--- true
StringUtils.startsWithIgnoreCase(null,"ab"); //--- false
str - 字符串prefix - 给定的字符串,以该字符串开头prefix开头(忽略大小写)则返回true,否则返回falsepublic static boolean endsWithIgnoreCase(String str, String suffix)
判断字符串中是否以给定的字符串结尾,不考虑大小写问题.
StringUtils.endsWithIgnoreCase("ddcbab",null); //--- false
StringUtils.endsWithIgnoreCase("ddcbab","ab"); //--- true
StringUtils.endsWithIgnoreCase("ddcbab","bc"); //--- false
StringUtils.endsWithIgnoreCase("ddcbab","AB"); //--- true
StringUtils.endsWithIgnoreCase(null,"ab"); //--- false
str - 字符串suffix - 给定的字符串,以该字符串开头prefix开头(忽略大小写)则返回true,否则返回falseString.endsWith(String)public static String replace(String str, String oldPattern, String newPattern)
将字符串中所有出现oldPattern替换为newPattern.
StringUtils.replace("com.mzlion.utility.","l","x"); //--- "hexxo worxd"
str - 字符串oldPattern - 需要替换的字符串newPattern - 新的字符串public static String delete(String str, String pattern)
将字符串str中的出现了指定子串pattern全部删除,删除的字符串不支持正则表达式.
StringUtils.delete("hello world","l"); //--- "heo word";
str - 字符串pattern - 要删除的字符串public static String capitalize(String str)
将字符串的第一个字符(必须在Character.toUpperCase(char)中,否则就不会改变)转换为大写.
StringUtils.capitalize("she si just a kid"); //--- "She is just a kid"
str - 字符串public static String unCapitalize(String str)
将字符串的第一个字符(必须在Character.toLowerCase(char)中,否则就不会改变)转换为小写.
StringUtils.unCapitalize("Hello"); //--- "hello"
str - 字符串public static String changeCharacterCase(String str, int index, boolean capital)
index)的字符(必须在Character.toLowerCase(char))转为大(小)写,否则就不会改变
StringUtils.changeCharacterCase("Hello",2,true); //--- "HeLlo"
str - 字符串index - 大小写更改位置capital - 大写还是小写,当值为true时则大写,值为false时则小写public static String[] splitAtFirst(String toSplit, String delimiter)
toSplit - 要分隔的字符串delimiter - 分隔符public static String[] split(String toSplit)
toSplit - 待分割的字符串split(String, String)public static String[] split(String toSplit, String delimiter)
toSplit - 要分隔的字符串delimiter - 分隔符StringTokenizerpublic static String toUnderline(String name)
例如:HelloWorld to hello_world
name - 驼峰式的字符串public static String toUnderline(String name, boolean upper)
name - 驼峰式的字符串upper - 如果为true则将字符串均转为大写Copyright © 2017. All rights reserved.