来源:http://blog.sina.com.cn/s/blog_621b6f0e0100tqaj.html

org.springframework.util.StringUtils

我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。
工具类整理如下:
  StringUtils.hasLength(null) = false
  StringUtils.hasLength("") = false
  StringUtils.hasLength(" ") = true
  StringUtils.hasLength("Hello") = true
 
   StringUtils.hasText(null) = false
   StringUtils.hasText("") = false
   StringUtils.hasText(" ") = false
   StringUtils.hasText("12345") = true
   StringUtils.hasText(" 12345 ") = true
 //是否包含空白字符
 StringUtils.containsWhitespace(null)=false
 StringUtils.containsWhitespace("")=false
 StringUtils.containsWhitespace("a")=false
 StringUtils.containsWhitespace("abc")=false
 StringUtils.containsWhitespace("abc")=false
 StringUtils.containsWhitespace(" ")=true
 StringUtils.containsWhitespace(" a")=true
 StringUtils.containsWhitespace("abc ")=true
 StringUtils.containsWhitespace("a b")=true
 StringUtils.containsWhitespace("a  b")

StringUtils.trimWhitespace(null)=null;
 StringUtils.trimWhitespace("")="";
 StringUtils.trimWhitespace(" ")="";
 StringUtils.trimWhitespace("\t")="";
 StringUtils.trimWhitespace(" a")="a";
 StringUtils.trimWhitespace("a ")="a";
 StringUtils.trimWhitespace(" a ")="a";
 StringUtils.trimWhitespace(" a b ")="a b";

StringUtils.trimLeadingWhitespace(null)=null;
 StringUtils.trimLeadingWhitespace("")="";
 StringUtils.trimLeadingWhitespace(" ")="";
 StringUtils.trimLeadingWhitespace("\t")="";
 StringUtils.trimLeadingWhitespace(" a")="a";
 StringUtils.trimLeadingWhitespace("a ")="a ";
 StringUtils.trimLeadingWhitespace(" a ")="a ";
 StringUtils.trimLeadingWhitespace(" a b ")="a b "
 StringUtils.trimLeadingWhitespace(" a b  c ")="a b  c "

StringUtils.trimTrailingWhitespace(null)=null;
 StringUtils.trimTrailingWhitespace(" ")="";
 StringUtils.trimTrailingWhitespace("\t")="";
 StringUtils.trimTrailingWhitespace("a ")="a";
 StringUtils.trimTrailingWhitespace(" a")=" a";
 StringUtils.trimTrailingWhitespace(" a ")=" a";
 StringUtils.trimTrailingWhitespace(" a b ")=" a b";
 StringUtils.trimTrailingWhitespace(" a b  c ")=" a b  c";

StringUtils.trimAllWhitespace("")="";
 StringUtils.trimAllWhitespace(" ")="";
 StringUtils.trimAllWhitespace("\t")="";
 StringUtils.trimAllWhitespace(" a")="a";
 StringUtils.trimAllWhitespace("a ")="a";
 StringUtils.trimAllWhitespace(" a ")="a";
 StringUtils.trimAllWhitespace(" a b ")="ab";
 StringUtils.trimAllWhitespace(" a b  c "="abc";
 // 统计一个子字符串在字符串出现的次数
 StringUtils.countOccurrencesOf(null, null) == 0;
 StringUtils.countOccurrencesOf("s", null) == 0;
 StringUtils.countOccurrencesOf(null, "s") == 0;
 StringUtils.countOccurrencesOf("erowoiueoiur", "WERWER") == 0;
 StringUtils.countOccurrencesOf("erowoiueoiur", "x")=0;
 StringUtils.countOccurrencesOf("erowoiueoiur", " ") == 0;
 StringUtils.countOccurrencesOf("erowoiueoiur", "") == 0;
 StringUtils.countOccurrencesOf("erowoiueoiur", "e") == 2;
 StringUtils.countOccurrencesOf("erowoiueoiur", "oi") == 2;
 StringUtils.countOccurrencesOf("erowoiueoiur", "oiu") == 2;
 StringUtils.countOccurrencesOf("erowoiueoiur", "oiur") == 1;
 StringUtils.countOccurrencesOf("erowoiueoiur", "r") == 2;

//字符串替换
 String inString = "a6AazAaa77abaa";
 String oldPattern = "aa";
 String newPattern = "foo";
 // Simple replace
 String s = StringUtils.replace(inString, oldPattern, newPattern);
 s.equals("a6AazAfoo77abfoo")=true;

// Non match: no change
 s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
 s.equals(inString)=true
 s = StringUtils.replace(inString, oldPattern, null);
 s.equals(inString)=true

// Null old pattern: should ignore
 s = StringUtils.replace(inString, null, newPattern);
        s.equals(inString)=true
 //删除字符串

String inString = "The quick brown fox jumped over the lazy dog";
 String noThe = StringUtils.delete(inString, "the");
 noThe.equals("The quick brown fox jumped over  lazy dog")=true;
 String nohe = StringUtils.delete(inString, "he");
 nohe.equals("T quick brown fox jumped over t lazy dog")=true;
 String nosp = StringUtils.delete(inString, " ");
 nosp.equals("Thequickbrownfoxjumpedoverthelazydog")=true;
 String killEnd = StringUtils.delete(inString, "dog");
 killEnd.equals("The quick brown fox jumped over the lazy ")=true;
 String mismatch = StringUtils.delete(inString, "dxxcxcxog");
  mismatch.equals(inString)=true;

//删除任何字符
 //源代码如下
 //char c = inString.charAt(i);
 //如果不存在 c 值,则返回 -1
 //if (charsToDelete.indexOf(c) == -1) {
 //out.append(c);
 //}

String inString = "Able was I ere I saw Elba";

String res = StringUtils.deleteAny(inString, "I");
        res.equals("Able was  ere  saw Elba")=true;
 res = StringUtils.deleteAny(inString, "AeEba!");
 res.equals("l ws I r I sw l")=true;
 String mismatch = StringUtils.deleteAny(inString, "#@$#$^");
 mismatch.equals(inString)=true;

//源代码如下 return (str != null ? "'" + str + "'" : null);
 assertEquals("'myString'", StringUtils.quote("myString"));
 assertEquals("''", StringUtils.quote(""));
 assertNull(StringUtils.quote(null));
 //将第一个字符改大写
 StringUtils.capitalize(Str)
 //将第一个个字符改小写
 StringUtils.uncapitalize(str)

//mypath/myfile.txt" -> "myfile.txt
 //获取字符串文件名和扩展名
 StringUtils.getFilename("myfile").equals("myfile")=true;
 StringUtils.getFilename("mypath/myfile".equals("myfile")=true;
 StringUtils.getFilename("mypath/myfile".equals("myfile")=true;
 StringUtils.getFilename("myfile.txt").equals("myfile.txt")=true;
 StringUtils.getFilename("mypath/myfile.txt").equals("myfile.txt")=true;
 // 获取字符串扩展名,以.分隔
 StringUtils.getFilenameExtension("myfile")=null;
 StringUtils.getFilenameExtension("myPath/myfile")=null;
 StringUtils.getFilenameExtension("myfile.").equals("")=true;
 StringUtils.getFilenameExtension("myPath/myfile.").equals("")=true;
 StringUtils.StringUtils.getFilenameExtension("myfile.txt").equals("txt")=true;
 StringUtils.getFilenameExtension("mypath/myfile.txt").equals("txt")=true;

//舍去文件名扩展名
 StringUtils.stripFilenameExtension(null)=true;
 StringUtils.stripFilenameExtension("").equals("")=true;
 StringUtils.stripFilenameExtension("myfile").equals("myfile")=true;
 StringUtils.stripFilenameExtension("mypath/myfile").equals("mypath/myfile")=true;
 StringUtils.stripFilenameExtension("myfile.").equals("myfile")=true;
 StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;
 StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;
 StringUtils.stripFilenameExtension("myfile.txt").equals("myfile")=true;
 StringUtils.stripFilenameExtension("mypath/myfile.txt").equals("mypath/myfile")=true

来源:http://blog.sina.com.cn/s/blog_621b6f0e0100tqaj.html

StringUtils测试相关推荐

  1. StringUtils常用方法+StringUtils详细介绍

    StringUtils用法+StringUtils详细介绍 博文来源:http://yijianfengvip.blog.163.com/blog/static/1752734322012122219 ...

  2. StringUtils详解

    public static void StringUtil(){//null 和 ""操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//判断是否Nu ...

  3. freemarker的测试结果框架_TestNG框架Listener介绍及测试结果的收集

    此文已由作者范旭斐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 抛砖引玉 假设我们将testng作为自动化测试框架的选型方案,以下两个问题如何实现: 问题1:如何将每次执行 ...

  4. commons-lang3之StringUtils

    字符串是一种在开发中经常使用到的数据类型,对字符串的处理也变得非常重要,字符串本身有一些方法,但都没有对null做处理,而且有时可能还需要做一些额外处理才能满足我们的需求,比如,要判断某个字符串中是否 ...

  5. 自定义ImageView 实现双击放大缩小还原,无极缩小和旋转及拖动(多机型测试很稳定)

    /*** 该模块主要实现了放大和原大两个级别的缩放. 功能有: 1.以触摸点为中心放大(这个是网上其他的代码没有的) 2.取消边界控制(这个是网上其他的代码没有的)也可以添加边界控制 3.双击放大或缩 ...

  6. java api 测试工具_Java 实现在线HTTP接口测试 - HTTP GET/POST模拟请求测试工具

    本站工具,在线HTTP接口测试 - HTTP GET/POST模拟请求测试工具后台所有实现代码.支持的请求协议有:Post.GetDelete.Put.Trace.Head.Options.特色是加了 ...

  7. guava和commons_使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在...

    guava和commons 最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,&quo ...

  8. stringutils_番石榴分配器vs StringUtils

    stringutils 因此,我最近写了一篇关于旧的,可靠的Apache Commons StringUtils的文章 ,该文章引起了很多评论,其中之一是Google Guava提供了更好的连接和拆分 ...

  9. 使用Guava CharMatcher和Apache Commons Lang StringUtils确定字符串中字符或整数的存在

    最近Reddit上的帖子提出了一个问题:" 是否存在一种预定义的方法来检查变量值是否包含特定字符或整数? "基于问题的标题也被以另一种方式问到,"一种检查变量是否包含诸如 ...

  10. 番石榴分配器vs StringUtils

    因此,我最近写了一篇有关旧的,可靠的Apache Commons StringUtils的文章 ,该文章引起了一些评论,其中之一是Google Guava提供了更好的连接和拆分字符串的机制. 我必须承 ...

最新文章

  1. Maven学习六之利用mvn deploy命令上传包
  2. python 日志输出模块--两种方法
  3. mysql 自定义抛出异常_C#自定义异常(throw抛出异常)
  4. 我就改了一行代码,为什么就全超时了?
  5. Linux如何指向mysql_linux的基本操作(mysql 的基本操作)
  6. how drop down list description is displayed by UI framework
  7. AUTOSAR从入门到精通100讲(四)-CAN总线数据帧分类及格式详解
  8. docker nginx:1.21.4
  9. pytorch_basics Save and load model
  10. 洛谷 P3496 [POI2010]GIL-Guilds 题解
  11. 什么是JSTL和EL表达式
  12. 猜游戏程序java_java猜字游戏
  13. 数字全角转半角VBA
  14. 民航飞行学院计算机研究生就业,数据说话:文科硕士研究生就业变迁史
  15. 【OpenPrompt】源码学习笔记
  16. XCTF Guess-the-Number
  17. [插件发布] KK_XSHOW首页多格(Discuz) DX2.0 首款支持x2的首页N格焦点图!
  18. 1.7Hadoop-HDFS命令
  19. 公司开源的java分词,Java开源项目cws_evaluation:中文分词器分词效果评估
  20. Python爬虫 | 获取股票行业资金流向

热门文章

  1. PCL中的ICP算法(Registration模块之IterativeClosestPoint点云配准)
  2. 一文解决样本不均衡(全)
  3. [BZOJ1085][SCOI2005]骑士精神
  4. Spoj REPEATS 后缀自动机+set
  5. [深入React] 8.refs
  6. 完成3DM以后的总结(2).Xutils的简单使用
  7. MVC视图与控制器分离简单描述
  8. 在 Panorama 页面中添加 ApplicationBar
  9. Jquery简单幻灯片
  10. ASP.NET MVC 3和Razor中的@helper 语法