StringUtils测试

StringUtils测试来源:http://blog.sina.com.cn/s/blog_621b6f0e0100tqaj.htmlorg.springframework.util.StringUtils我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils工具类帮我们处理字符串。工具类整理如下:

大家好,又见面了,我是你们的朋友全栈君。

来源: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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/136664.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 求二叉树的最长路径_对下列二叉树进行前序遍历的结果为

    求二叉树的最长路径_对下列二叉树进行前序遍历的结果为Ural 大学有 N 名职员,编号为 1∼N。他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司。每个职员有一个快乐指数,用整数 Hi 给出,其中 1≤i≤N。现在要召开一场周年庆宴会,不过,没有职员愿意和直接上司一起参会。在满足这个条件的前提下,主办方希望邀请一部分职员参会,使得所有参会职员的快乐指数总和最大,求这个最大值。输入格式第一行一个整数 N。接下来 N 行,第 i 行表示 i 号职员的快乐指数 Hi。接下来 N−1 行,每行输入一对整数 L,K,表示 K 是 L 的直接

    2022年8月9日
    7
  • 织梦dede如何去除Power by DedeCms

    织梦dede如何去除Power by DedeCms自从dedecms织梦系统更新到6.7日的版本,底部版权信息调用标签{dede:global.cfg_powerby/}会自动加上织梦官方的链接[Power by DedeCms ],想必很多新用户使用中都想去除这个官方的链接,由于这是官方的作为,本文只是提供一个技术的交流,并不鼓励大家去除底部的链接。好了,回归正题,大家知道,进入后台在后台的系统-系统参数有个网站版权信息,在这里并不能找到官方的

    2022年7月15日
    12
  • SPSS步骤|卡方检验详细操作和结果分析「建议收藏」

    SPSS步骤|卡方检验详细操作和结果分析「建议收藏」​卡方检验是很常用的一种分析方法,什么情况下使用卡方检验?如果你手上的数据是一种定类数据,比如性别(男、女)是否患病(是、否)。你还想要分析定类数据和定类数据之间的差异关系。例如想要分析性别和是否抽烟之间的关系。这一句话里面包含两个词语,分别是:性别,是否抽烟。性别为X,是否抽烟为Y。性别为定类数据,是否抽烟也是定类数据,此时就可以使用卡方检验。这篇文章分享分别使用两种常见统计分析工具SPSS和SPSSAU完成卡方检验。SPSS是目前常用的统计软件,SPSSAU是更简单的在线数据科学分析工具

    2022年5月17日
    121
  • Springboot + Openjpa 整合 GBase8s 实践「建议收藏」

    Springboot + Openjpa 整合 GBase8s 实践「建议收藏」本文我们将先来介绍JPA以及OpenJPA之间的关系,然后通过一个手把手的应用案例来讲述Springboot和Openjpa整合GBase8s。那么就让我们开始吧。JPAJPA(JavaPersistenceAPI)作为JavaEE5.0平台标准的ORM规范,将得到所有JavaEE服务器的支持。Sun这次吸取了之前EJB规范惨痛失败的经历,在充分吸收现有ORM框架的基础上,得到了一个易于使用、伸缩性强的ORM规范。从目前的开发社区的反应上看,JPA受到了极大…

    2022年4月28日
    76
  • windows 怎样关闭redis

    windows 怎样关闭redis

    2021年10月16日
    204
  • 为什么百度查到的ip和ipconfig查到的不一样;详解公网Ip和私网ip;详解网络分类ABC;

    为什么百度查到的ip和ipconfig查到的不一样;详解公网Ip和私网ip;详解网络分类ABC; IP可以分为PublicIP和PrivateIP,出现这种规划的原因在于IPv4所能表示的IP太少而电脑太多以至于不够用,然而只有PublicIP才能直接连接上网络,所以对于那些公司,学校,政府机构等场所,就可以集中使用私有的IP进行管理,而大家可以共用一个IP去连接上公网,这样,就省下了许多宝贵的PublicIP。你有没有发现,你每次使用ipconfig查到的地址,要么就是172….

    2022年6月6日
    128

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号