飞道的博客

Springboot内置的工具类之StringUtils

318人阅读  评论(0)

        在实际的业务开发中,除了经常有针对对象的判断或操作以外,经常也会遇到的就是字符串的判断和操作。比如判断字符串是否为空、是否以某个字符结尾、去除头部和尾部的空白字符、字符的查找和替换。在Spring的核心包中存在这样一个类org.springframework.util.StringUtils,它提供了常见的关于字符串的判断和操作的静态方法。下面咱们针对一些常见的一块学习一下,顺便再把前面说的断言给复习一下:

org.springframework.util.StringUtils

字符串判断

 判断字符串是否为 null,或 ""。注意,包含空白符的字符串为非空。

boolean isEmpty(Object str)


  
  1. @Test
  2. public void test( ) {
  3. String name = "";
  4. boolean empty = StringUtils. isEmpty(name);
  5. Assert. isTrue(empty, "变量name是有值的");
  6. System. out. println( "给name赋值为zhangsan");
  7. name = "zhangsan";
  8. boolean empty1 = StringUtils. isEmpty(name);
  9. Assert. isTrue(empty1, "变更name是有值的");
  10. }

判断字符串是否是以指定内容结束。忽略大小写。

boolean endsWithIgnoreCase(String str, String suffix)


  
  1. @Test
  2. public void test2( ) {
  3. String name = "zhangsanAK";
  4. boolean ak = StringUtils. endsWithIgnoreCase(name, "ak");
  5. Assert. isTrue(ak, "变量name不是以ak结尾的");
  6. boolean akb = StringUtils. endsWithIgnoreCase(name, "akb");
  7. Assert. isTrue(akb, "变量name不是以akb结尾的");
  8. }

判断字符串是否已指定内容开头。忽略大小写。

boolean startsWithIgnoreCase(String str, String prefix)


  
  1. @Test
  2. public void test3( ) {
  3. String name = "OKzhangsan";
  4. boolean ok = StringUtils. startsWithIgnoreCase(name, "ok");
  5. Assert. isTrue(ok, "变量name不是以ak结尾的");
  6. boolean abc = StringUtils. startsWithIgnoreCase(name, "abc");
  7. Assert. isTrue(abc, "变量name不是以abc开始的");
  8. }

是否包含空白符。

boolean containsWhitespace(String str)


  
  1. @Test
  2. public void test4( ) {
  3. String name = "OK zhangsan";
  4. boolean spaceFlag = StringUtils. containsWhitespace(name);
  5. Assert. isTrue(spaceFlag, "变量name没有包含空格");
  6. System. out. println( "给name赋值为zhangsan");
  7. name = "zhangsan";
  8. boolean spaceFlag2 = StringUtils. containsWhitespace(name);
  9. Assert. isTrue(spaceFlag2, "变量name没有包含空格");
  10. }

判断字符串非空且长度不为 0,即,Not Empty。

boolean hasLength(CharSequence str)


  
  1. @Test
  2. public void test5( ) {
  3. String name = "zhangsan";
  4. boolean lengthFlag = StringUtils. hasLength(name);
  5. Assert. isTrue(lengthFlag, "变量name非空,但是长度为0");
  6. System. out. println( "给name赋值为一个空字符串");
  7. name = " ";
  8. boolean lengthFlag2 = StringUtils. hasLength(name);
  9. Assert. isTrue(lengthFlag2, "变量name非空,但是长度为0");
  10. System. out. println( "给name赋值为一个空字符串,但长度为0");
  11. name = "";
  12. boolean lengthFlag3 = StringUtils. hasLength(name);
  13. Assert. isTrue(lengthFlag3, "变量name非空,但是长度为0");
  14. }

判断字符串是否包含实际内容,即非仅包含空白符,也就是 Not Blank。这里要特别注意一下,通常empty是指null、“”,“ ”,多个空字符就不会认为是Empty了,而是Blank;

boolean hasText(CharSequence str)


  
  1. @Test
  2. public void test6( ) {
  3. String name = "zhangsan";
  4. boolean lengthFlag = StringUtils. hasText(name);
  5. Assert. isTrue(lengthFlag, "变量name非空,但是长度为0");
  6. System. out. println( "给name赋值为一个空字符串");
  7. name = " ";
  8. boolean lengthFlag2 = StringUtils. hasText(name);
  9. Assert. isTrue(lengthFlag2, "变量是一个空字符");
  10. }

判断字符串指定索引处是否包含一个子串。

boolean substringMatch(CharSequence str, int index, CharSequence substring)


  
  1. @Test
  2. public void test7( ) {
  3. String msg = "I am zhangsan";
  4. boolean lengthFlag = StringUtils. substringMatch(msg, 5, "zhangsan");
  5. Assert. isTrue(lengthFlag, "变量msg的索引位置为5的地方,是zhangsan");
  6. msg = "I am not zhangsan";
  7. boolean lengthFlag2 = StringUtils. substringMatch(msg, 5, "zhangsan");
  8. Assert. isTrue(lengthFlag2, "变量msg的索引位置为5的地方,是zhangsan");
  9. }

计算一个字符串中指定子串的出现次数。

int countOccurrencesOf(String str, String sub)


  
  1. @Test
  2. public void test8 () {
  3. String msg = "aaa";
  4. int count = StringUtils.countOccurrencesOf(msg, "a");
  5. Assert.isTrue(count == 3, "变量msg中没有包含3个a");
  6. msg = "bbb";
  7. int count2 = StringUtils.countOccurrencesOf(msg, "b");
  8. Assert.isTrue(count2== 4, "变量msg中没有包含4个b");
  9. }

字符串操作

查找并替换指定子串。

String replace(String inString, String oldPattern, String newPattern)


  
  1. @Test
  2. public void test10( ) {
  3. String name = "aba";
  4. String name2 = StringUtils. replace(name, "b", "a");
  5. Assert. isTrue(name2. equals( "aaa"), "变量name替换b后结果不是aaa");
  6. }

去除尾部的特定字符。

String trimTrailingCharacter(String str, char trailingCharacter)


  
  1. @Test
  2. public void test11( ) {
  3. String name = "aba";
  4. String a = StringUtils. trimTrailingCharacter(name, 'a');
  5. Assert. isTrue(a. equals( "ab"), "去除尾部的a后不是ab");
  6. }

去除头部的特定字符。

String trimLeadingCharacter(String str, char leadingCharacter)


  
  1. @Test
  2. public void test12( ) {
  3. String name = "aba";
  4. String a = StringUtils. trimLeadingCharacter(name, 'a');
  5. Assert. isTrue(a. equals( "ba"), "去除开头的a后不是ba");
  6. }

去除头部的空白符。

String trimLeadingWhitespace(String str)


  
  1. @Test
  2. public void test13( ) {
  3. String name = "aba ";
  4. String a = StringUtils. trimTrailingWhitespace(name);
  5. Assert. isTrue(a. equals( "aba"), "去除尾部的空格失败了");
  6. }

去除头部的空白符。

String trimTrailingWhitespace(String str)


  
  1. @Test
  2. public void test14( ) {
  3. String name = " aba";
  4. String a = StringUtils. trimLeadingWhitespace(name);
  5. Assert. isTrue(a. equals( "aba"), "去除开头的空格失败了");
  6. }

去除头部和尾部的空白符。

String trimWhitespace(String str)


  
  1. @Test
  2. public void test15( ) {
  3. String name = " aba ";
  4. String a = StringUtils. trimWhitespace(name);
  5. Assert. isTrue(a. equals( "aba"), "去除开头和尾部的空格失败了");
  6. }

删除开头、结尾和中间的空白符。

String trimAllWhitespace(String str)


  
  1. @Test
  2. public void test16( ) {
  3. String name = " a b a ";
  4. String a = StringUtils. trimAllWhitespace(name);
  5. Assert. isTrue(a. equals( "aba"), "去除开头、中间、尾部的空格失败了");
  6. }

删除指定子串。

String delete(String inString, String pattern)


  
  1. @Test
  2. public void test17( ) {
  3. String name = "abcabc";
  4. String a = StringUtils. delete(name, "bc");
  5. Assert. isTrue(a. equals( "aa"), "删除变更name里的字符b失败了");
  6. }

对数组的每一项执行 trim() 方法。

String[] trimArrayElements(String[] array)


  
  1. @Test
  2. public void test18( ) {
  3. String[] names = { " zhangsan " , " li si "};
  4. String[] arr = StringUtils. trimArrayElements(names);
  5. for ( String s : arr) {
  6. Assert. isTrue(s. equals( "zhangsan")||s. equals( "lisi"), "字符"+s+ "中没有包含zhangsan或lisi");
  7. }
  8. }

将 URL 字符串进行解码。

String uriDecode(String source, Charset charset)


  
  1. @Test
  2. public void test19( ) {
  3. String result= "http://localhost:8080/abc?name=zhangsan";
  4. String url= "http%3A%2F%2Flocalhost%3A8080%2Fabc%3Fname%3Dzhangsan";
  5. String tmp = StringUtils. uriDecode(url, Charset. forName( "utf-8"));
  6. Assert. isTrue(result. equals(tmp), "解码失败");
  7. }

        能看到最后一行的小伙们都是求知欲爆棚的,那就动动发财小手点个赞加个送注呗,收藏+关注,永远不迷路!


转载:https://blog.csdn.net/fox9916/article/details/128306531
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场