在实际的业务开发中,除了经常有针对对象的判断或操作以外,经常也会遇到的就是字符串的判断和操作。比如判断字符串是否为空、是否以某个字符结尾、去除头部和尾部的空白字符、字符的查找和替换。在Spring的核心包中存在这样一个类org.springframework.util.StringUtils,它提供了常见的关于字符串的判断和操作的静态方法。下面咱们针对一些常见的一块学习一下,顺便再把前面说的断言给复习一下:
org.springframework.util.StringUtils
字符串判断
判断字符串是否为 null,或 ""。注意,包含空白符的字符串为非空。
boolean isEmpty(Object str)
-
@Test
-
public
void
test(
) {
-
String name =
"";
-
boolean empty =
StringUtils.
isEmpty(name);
-
Assert.
isTrue(empty,
"变量name是有值的");
-
System.
out.
println(
"给name赋值为zhangsan");
-
name =
"zhangsan";
-
boolean empty1 =
StringUtils.
isEmpty(name);
-
Assert.
isTrue(empty1,
"变更name是有值的");
-
}
判断字符串是否是以指定内容结束。忽略大小写。
boolean endsWithIgnoreCase(String str, String suffix)
-
@Test
-
public
void
test2(
) {
-
String name =
"zhangsanAK";
-
boolean ak =
StringUtils.
endsWithIgnoreCase(name,
"ak");
-
Assert.
isTrue(ak,
"变量name不是以ak结尾的");
-
boolean akb =
StringUtils.
endsWithIgnoreCase(name,
"akb");
-
Assert.
isTrue(akb,
"变量name不是以akb结尾的");
-
}
判断字符串是否已指定内容开头。忽略大小写。
boolean startsWithIgnoreCase(String str, String prefix)
-
@Test
-
public
void
test3(
) {
-
String name =
"OKzhangsan";
-
boolean ok =
StringUtils.
startsWithIgnoreCase(name,
"ok");
-
Assert.
isTrue(ok,
"变量name不是以ak结尾的");
-
boolean abc =
StringUtils.
startsWithIgnoreCase(name,
"abc");
-
Assert.
isTrue(abc,
"变量name不是以abc开始的");
-
}
是否包含空白符。
boolean containsWhitespace(String str)
-
@Test
-
public
void
test4(
) {
-
String name =
"OK zhangsan";
-
boolean spaceFlag =
StringUtils.
containsWhitespace(name);
-
Assert.
isTrue(spaceFlag,
"变量name没有包含空格");
-
System.
out.
println(
"给name赋值为zhangsan");
-
name =
"zhangsan";
-
boolean spaceFlag2 =
StringUtils.
containsWhitespace(name);
-
Assert.
isTrue(spaceFlag2,
"变量name没有包含空格");
-
}
判断字符串非空且长度不为 0,即,Not Empty。
boolean hasLength(CharSequence str)
-
@Test
-
public
void
test5(
) {
-
String name =
"zhangsan";
-
boolean lengthFlag =
StringUtils.
hasLength(name);
-
Assert.
isTrue(lengthFlag,
"变量name非空,但是长度为0");
-
System.
out.
println(
"给name赋值为一个空字符串");
-
name =
" ";
-
boolean lengthFlag2 =
StringUtils.
hasLength(name);
-
Assert.
isTrue(lengthFlag2,
"变量name非空,但是长度为0");
-
System.
out.
println(
"给name赋值为一个空字符串,但长度为0");
-
name =
"";
-
boolean lengthFlag3 =
StringUtils.
hasLength(name);
-
Assert.
isTrue(lengthFlag3,
"变量name非空,但是长度为0");
-
}
判断字符串是否包含实际内容,即非仅包含空白符,也就是 Not Blank。这里要特别注意一下,通常empty是指null、“”,“ ”,多个空字符就不会认为是Empty了,而是Blank;
boolean hasText(CharSequence str)
-
@Test
-
public
void
test6(
) {
-
String name =
"zhangsan";
-
boolean lengthFlag =
StringUtils.
hasText(name);
-
Assert.
isTrue(lengthFlag,
"变量name非空,但是长度为0");
-
System.
out.
println(
"给name赋值为一个空字符串");
-
name =
" ";
-
boolean lengthFlag2 =
StringUtils.
hasText(name);
-
Assert.
isTrue(lengthFlag2,
"变量是一个空字符");
-
}
判断字符串指定索引处是否包含一个子串。
boolean substringMatch(CharSequence str, int index, CharSequence substring)
-
@Test
-
public
void
test7(
) {
-
String msg =
"I am zhangsan";
-
boolean lengthFlag =
StringUtils.
substringMatch(msg,
5,
"zhangsan");
-
Assert.
isTrue(lengthFlag,
"变量msg的索引位置为5的地方,是zhangsan");
-
msg =
"I am not zhangsan";
-
boolean lengthFlag2 =
StringUtils.
substringMatch(msg,
5,
"zhangsan");
-
Assert.
isTrue(lengthFlag2,
"变量msg的索引位置为5的地方,是zhangsan");
-
}
计算一个字符串中指定子串的出现次数。
int countOccurrencesOf(String str, String sub)
-
@Test
-
public
void
test8
() {
-
String
msg
=
"aaa";
-
int
count
= StringUtils.countOccurrencesOf(msg,
"a");
-
Assert.isTrue(count ==
3,
"变量msg中没有包含3个a");
-
msg =
"bbb";
-
int
count2
= StringUtils.countOccurrencesOf(msg,
"b");
-
Assert.isTrue(count2==
4,
"变量msg中没有包含4个b");
-
}
字符串操作
查找并替换指定子串。
String replace(String inString, String oldPattern, String newPattern)
-
@Test
-
public
void
test10(
) {
-
String name =
"aba";
-
String name2 =
StringUtils.
replace(name,
"b",
"a");
-
Assert.
isTrue(name2.
equals(
"aaa"),
"变量name替换b后结果不是aaa");
-
}
去除尾部的特定字符。
String trimTrailingCharacter(String str, char trailingCharacter)
-
@Test
-
public
void
test11(
) {
-
String name =
"aba";
-
String a =
StringUtils.
trimTrailingCharacter(name,
'a');
-
Assert.
isTrue(a.
equals(
"ab"),
"去除尾部的a后不是ab");
-
}
去除头部的特定字符。
String trimLeadingCharacter(String str, char leadingCharacter)
-
@Test
-
public
void
test12(
) {
-
String name =
"aba";
-
String a =
StringUtils.
trimLeadingCharacter(name,
'a');
-
Assert.
isTrue(a.
equals(
"ba"),
"去除开头的a后不是ba");
-
}
去除头部的空白符。
String trimLeadingWhitespace(String str)
-
@Test
-
public
void
test13(
) {
-
String name =
"aba ";
-
String a =
StringUtils.
trimTrailingWhitespace(name);
-
Assert.
isTrue(a.
equals(
"aba"),
"去除尾部的空格失败了");
-
}
去除头部的空白符。
String trimTrailingWhitespace(String str)
-
@Test
-
public
void
test14(
) {
-
String name =
" aba";
-
String a =
StringUtils.
trimLeadingWhitespace(name);
-
Assert.
isTrue(a.
equals(
"aba"),
"去除开头的空格失败了");
-
}
去除头部和尾部的空白符。
String trimWhitespace(String str)
-
@Test
-
public
void
test15(
) {
-
String name =
" aba ";
-
String a =
StringUtils.
trimWhitespace(name);
-
Assert.
isTrue(a.
equals(
"aba"),
"去除开头和尾部的空格失败了");
-
}
删除开头、结尾和中间的空白符。
String trimAllWhitespace(String str)
-
@Test
-
public
void
test16(
) {
-
String name =
" a b a ";
-
String a =
StringUtils.
trimAllWhitespace(name);
-
Assert.
isTrue(a.
equals(
"aba"),
"去除开头、中间、尾部的空格失败了");
-
}
删除指定子串。
String delete(String inString, String pattern)
-
@Test
-
public
void
test17(
) {
-
String name =
"abcabc";
-
String a =
StringUtils.
delete(name,
"bc");
-
Assert.
isTrue(a.
equals(
"aa"),
"删除变更name里的字符b失败了");
-
}
对数组的每一项执行 trim() 方法。
String[] trimArrayElements(String[] array)
-
@Test
-
public
void
test18(
) {
-
String[] names = {
" zhangsan " ,
" li si "};
-
String[] arr =
StringUtils.
trimArrayElements(names);
-
for (
String s : arr) {
-
Assert.
isTrue(s.
equals(
"zhangsan")||s.
equals(
"lisi"),
"字符"+s+
"中没有包含zhangsan或lisi");
-
}
-
}
将 URL 字符串进行解码。
String uriDecode(String source, Charset charset)
-
@Test
-
public
void
test19(
) {
-
String result=
"http://localhost:8080/abc?name=zhangsan";
-
String url=
"http%3A%2F%2Flocalhost%3A8080%2Fabc%3Fname%3Dzhangsan";
-
String tmp =
StringUtils.
uriDecode(url,
Charset.
forName(
"utf-8"));
-
Assert.
isTrue(result.
equals(tmp),
"解码失败");
-
}
能看到最后一行的小伙们都是求知欲爆棚的,那就动动发财小手点个赞加个送注呗,收藏+关注,永远不迷路!
转载:https://blog.csdn.net/fox9916/article/details/128306531