这些是个人,为了加强StringUtils方法的记忆,随便写写的,希望大家喜欢

public class StringUtilsTest extends TestCase {// 将字符串转换成小写 @Testpublic void test_lowerCase() {// assertEquals("aaa", StringUtils.lowerCase("AAA")); // assertEquals("", StringUtils.lowerCase("")); true // assertEquals("aa", StringUtils.lowerCase("Aa")); true // assertEquals("aa", StringUtils.lowerCase("aa")); true // System.out.println(StringUtils.lowerCase("")); assertEquals(null, StringUtils.lowerCase(null));}// 将字符串转换成大写 @Testpublic void test_upperCase() {// System.out.println(StringUtils.upperCase("aaa")); assertEquals("AAA", StringUtils.upperCase("aaa"));assertEquals(null, StringUtils.upperCase(null));}// 从第几个字符串开始省略 @Testpublic void test_abbreviate() {assertEquals("aa...", StringUtils.abbreviate("aaaa bbbb cccc", 5));// maxWidth最小为4 // System.out.println(StringUtils.abbreviate("aabbccddeeffgg", 6)); assertEquals(null, StringUtils.abbreviate(null, 4));}// 对字符串lenth对半分middle是中间分隔符 @Testpublic void test_abbreviateMiddle() {// System.out.println(StringUtils.abbreviateMiddle("abcdefghijk", "-", // 7)); assertEquals("ab-fg", StringUtils.abbreviateMiddle("abcdefg", "-", 5));}// 判断字符串是否为空或者是为null @Testpublic void test_isBlank() {assertEquals(false, StringUtils.isBlank("aaa"));assertEquals(true, StringUtils.isBlank(""));// System.out.println(StringUtils.isBlank("aaa")); }// 判断字符串非空或者是非null @Testpublic void test_isNotBlank() {assertEquals(false, StringUtils.isNotBlank(""));assertEquals(true, StringUtils.isNotBlank("adsfa"));}// 首写字母大写 @Testpublic void test_capitalize() {assertEquals("Aaa", StringUtils.capitalize("aaa"));assertEquals("", StringUtils.capitalize(""));}// 首写字母小写 @Testpublic void test_uncapitalize() {assertEquals("aAA", StringUtils.uncapitalize("AAA"));}// 以本在字符串在中间,两边用padStr来填充 @Testpublic void test_center() {// System.out.println(StringUtils.center("aaaaa", 10)); // System.out.println(StringUtils.center("aaa", 7, "--")); // assertEquals("--aa--", StringUtils.center("aa", 6, "--")); assertEquals(null, StringUtils.center(null, 6, "--"));}// 左侧填充 @Testpublic void test_leftPad() {assertEquals("--aa", StringUtils.leftPad("aa", 4, "-"));}// 右侧填充 @Testpublic void test_rightPad() {assertEquals("aa--", StringUtils.rightPad("aa", 4, "-"));}// 重复字符串 @Testpublic void test_repeat() {assertEquals("abcabc", StringUtils.repeat("abc", 2));assertEquals("abcbcabc", StringUtils.repeat("abc", "bc", 2));}// 是否全部小写 不能为null @Testpublic void test_isAllLowerCase() {assertEquals(true, StringUtils.isAllLowerCase("adfadfa"));}// 是否全部大写 不能为null @Testpublic void test_isAllUpperCase() {assertEquals(true, StringUtils.isAllUpperCase("AFADF"));// assertEquals(null, StringUtils.isAllUpperCase(null)); 不能为null }// 全部由字母组成 @Testpublic void test_isAlpha() {assertEquals(true, StringUtils.isAlpha("adsfad"));}// 全部由字母跟空格组成 @Testpublic void test_isAlphaSpace() {assertEquals(true, StringUtils.isAlphaSpace("afdsf   asfdad"));// assertEquals(null, StringUtils.isAlphaSpace(null)); 不能为null }// 只由字母和数字组成 @Testpublic void test_isAlphanumeric() {assertEquals(true, StringUtils.isAlphanumeric("adfad12321"));}// 只由字母数字和空格组成 @Testpublic void test_isAlphanumericSpace() {assertEquals(true, StringUtils.isAlphanumericSpace("adsfa  1232 adsf "));}// 只由数字 @Testpublic void test_isNumeric() {assertEquals(true, StringUtils.isNumeric("12312"));}// 只由数字和空格 @Testpublic void test_isNumericSpace() {assertEquals(true, StringUtils.isNumericSpace("12312   "));}// 小写字符串在大写字符串中的匹配次数 @Testpublic void test_countMatches() {assertEquals(3,StringUtils.countMatches("asdfasdfasdfasfdqwefvdasvfqwefadsvqwef","asdf"));// System.out.println(StringUtils.countMatches(//      "asdfasdfasdfasfdqwefvdasvfqwefadsvqwef", "asdf")); }// 字符串倒转 @Testpublic void test_reverse() {assertEquals("asdf", StringUtils.reverse("fdsa"));assertEquals(null, StringUtils.reverse(null));}// 大小写转换,空格不动 @Testpublic void test_swapCase() {assertEquals("aB - cD", StringUtils.swapCase("Ab - Cd"));}// 去掉两端控制字符 @Testpublic void test_trim() {// System.out.println(StringUtils.trim("  aa bb cc   \t")); assertEquals("", StringUtils.trim("        "));assertEquals("aa bb cc", StringUtils.trim("   aa bb cc \t \b"));assertEquals(null, StringUtils.trim(null));}// 去掉两端控制字符 跟 trim()方法是一个样的 @Testpublic void test_trimToNull() {// System.out.println(StringUtils.trim("  aa bb cc   \t")); assertEquals(null, StringUtils.trimToNull("   "));assertEquals("aa bb cc", StringUtils.trimToNull("   aa bb cc \t \b"));assertEquals(null, StringUtils.trim(null));}// 去掉两端控制字符 跟 trim()方法是一个样的 @Testpublic void test_trimToEmpty() {// System.out.println(StringUtils.trim("  aa bb cc   \t")); assertEquals("", StringUtils.trimToEmpty("  "));assertEquals("aa bb cc", StringUtils.trimToEmpty("   aa bb cc \t \b"));assertEquals(null, StringUtils.trim(null));assertEquals("", StringUtils.trim(""));}// 去掉字符串两端的空白符 @Testpublic void test_strip() {assertEquals("aa bb cc", StringUtils.strip("  aa bb cc    \t    "));}@Testpublic void test_stripToNull() {assertEquals("aa bb cc", StringUtils.stripToNull("  aa bb cc    \t    "));assertEquals(null, StringUtils.stripToNull(""));assertEquals(null, StringUtils.stripToNull(null));}@Testpublic void test_stripToEmpty() {assertEquals("aa bb cc", StringUtils.stripToEmpty("   aa bb cc "));assertEquals("", StringUtils.stripToEmpty(null));assertEquals("", StringUtils.stripToEmpty(""));}// 删除从结尾开始的小字符串 @Testpublic void test_stripEnd() {assertEquals("as", StringUtils.stripEnd("asdfghjkl", "dfghjkl"));}// 删除冲开头开始的小字符串 @Testpublic void test_stripStart() {assertEquals("kl", StringUtils.stripStart("asdfghjkl", "asdfghj"));assertEquals("", StringUtils.stripStart("", ""));assertEquals(null, StringUtils.stripStart(null, null));}//去掉每个元素开始和结尾的空格 @Testpublic void test_stripAll() {String[] temp1 = { "ads  ", " dsaf" };String[] temp2 = { "ads", "dsaf" };assertEquals("[ads, dsaf]",java.util.Arrays.toString(StringUtils.stripAll(temp1)));System.out.println(java.util.Arrays.toString(StringUtils.stripAll(temp1)));}// 无论大小写,对面是否相等 @Testpublic void test_equalsIgnoreCase() {assertEquals(true, StringUtils.equalsIgnoreCase("aA", "aa"));}// 验证字符出现的第一个位置 @Testpublic void test_indexOf() {assertEquals(1, StringUtils.indexOf("assddss", "s"));assertEquals(-1, StringUtils.indexOf("assddss", "2"));assertEquals(-1, StringUtils.indexOf(null, "2"));assertEquals(-1, StringUtils.indexOf("", "2"));assertEquals(5, StringUtils.indexOf("assddss", "s", 4));}@Testpublic void test_ordinalIndexOf() {assertEquals(14, StringUtils.ordinalIndexOf("aabbccddeeffggaa", "a", 3));}@Testpublic void test_contains() {assertEquals(true, StringUtils.contains("aabbccddeeffgg", "bb"));assertEquals(false, StringUtils.contains(null, ""));assertEquals(true, StringUtils.contains("", ""));assertEquals(false, StringUtils.contains("", null));assertEquals(false, StringUtils.contains(null, null));}@Testpublic void test_containsIgnoreCase() {assertEquals(true, StringUtils.containsIgnoreCase("ASDFghjKL", "FGHJ"));}// 小字符串在大字符串中的第一个出现的位置 @Testpublic void test_indexOfAny() {assertEquals(2, StringUtils.indexOfAny("asdfghjkl", "dfg"));}// 小字符串不在大字符串中出现的第一个位置 @Testpublic void test_indexOfAnyBut() {assertEquals(0, StringUtils.indexOfAnyBut("asdfghjkl", "dfg"));}// 大字符串是否仅仅只包含小字符串中的内容 @Testpublic void test_containsOnly() {assertEquals(true, StringUtils.containsOnly("adsfasdfas", "asdf"));}// 大字符串中不包含小字符串的内容 @Testpublic void test_containsNone() {assertEquals(false, StringUtils.containsNone("as", "adsf"));}// 小字符串在大字符串中最后一次出现的位置 @Testpublic void test_lastIndexOf() {assertEquals(8, StringUtils.lastIndexOf("aabbccedaa", "aa"));}// 从规定的下标数开始截取字符串 @Testpublic void test_substring() {assertEquals("sdf", StringUtils.substring("asdfgh", 1, 4));}// 在大字符串中存在于小字符串过后的内容 @Testpublic void test_substringAfter() {assertEquals("hjkl", StringUtils.substringAfter("asdfghjkl", "sdfg"));}// 在大字符串中存在于小字符串之前的内容 @Testpublic void test_substringBefore() {assertEquals("a", StringUtils.substringBefore("asdfghjkl", "sdfg"));}// 在大字符串中存在于两个小字符转中的内容 @Testpublic void test_substringBetween() {assertEquals("sd", StringUtils.substringBetween("asdfgh", "a", "f"));}// 在最后一个tag字符串的后面的内容 @Testpublic void test_substringAfterLast() {assertEquals("bb", StringUtils.substringAfterLast("tagabctagbb", "tag"));}// 在最后一个tag字符串前面的内容 @Testpublic void test_substringBeforeLast() {assertEquals("tagaatagbb",StringUtils.substringBeforeLast("tagaatagbbtagcc", "tag"));}// 输出多个字符串中间的内容 @Testpublic void test_substringsBetween() {System.out.println(StringUtils.substringsBetween("[a],[b],[c]", "[", "]"));}// 在左边的第几个字符的内容 @Testpublic void test_left() {assertEquals("as", StringUtils.left("asdfg", 2));assertEquals(null, StringUtils.left(null, 2));}// 对字符串右边第几个字符串的内容 @Testpublic void test_right() {assertEquals("fg", StringUtils.right("asdfg", 2));}// 省略字符串中的空格,然后用数组的形式输出 @Testpublic void test_split() {assertEquals("[aa, bb, cc]",java.util.Arrays.toString(StringUtils.split("aa bb cc   ")));}// 将字符串中不用一类型的,所有字符分割开来 @Testpublic void test_splitByCharacterType() {assertEquals("[number, 5]",java.util.Arrays.toString(StringUtils.splitByCharacterType("number5")));}// 规定分隔符 @Testpublic void test_splitByWholeSeparator() {assertEquals("[aa, bb, cc]",java.util.Arrays.toString(StringUtils.splitByWholeSeparator("aa bb cc", " ")));}@Testpublic void test_splitPreserveAllTokens() {assertEquals("[, aa, bb, , ccdd]",java.util.Arrays.toString(StringUtils.splitPreserveAllTokens(" aa bb  ccdd")));}// 把字符数的内容变成字符串 @Testpublic void test_join() {assertEquals("abc", StringUtils.join(new String[] { "a", "b", "c" }));}// 删除字符串黄总所有的空白字符 @Testpublic void test_deleteWhitespace() {assertEquals("abc", StringUtils.deleteWhitespace("a\n   \fb   \rc"));}// 删除指定的字符 @Testpublic void test_remove() {assertEquals("abde", StringUtils.remove("abcde", "c"));}// 只能删除最后的一节字符串 @Testpublic void test_removeEnd() {assertEquals("abc", StringUtils.removeEnd("abcde", "de"));assertEquals("abcde", StringUtils.removeEnd("abcde", "c"));}// 无论大小写删除最后一段字符 @Testpublic void test_removeEndIgnoreCase() {assertEquals("aBc", StringUtils.removeEndIgnoreCase("aBcDe", "de"));}@Testpublic void test_removeStart() {assertEquals("de", StringUtils.removeStart("abcde", "abc"));}@Testpublic void test_removeStartIgnoreCase() {assertEquals("CdE", StringUtils.removeStartIgnoreCase("AbCdE", "Ab"));}// 替换字符串中的指定字符 @Testpublic void test_replace() {assertEquals("adsfg", StringUtils.replace("asdfg", "sd", "ds"));}// 指定单个字符替换 @Testpublic void test_replaceChars() {assertEquals("bbcde", StringUtils.replaceChars("abcde", 'a', 'b'));}// 通过数组来一组替换 @Testpublic void test_replaceEach() {assertEquals("wcze",StringUtils.replaceEach("abcde", new String[] { "ab", "d" },new String[] { "w", "z" }));}// 只替换第一个找到的元素 @Testpublic void test_replaceOnce() {assertEquals("zbcad", StringUtils.replaceOnce("abcad", "a", "z"));}// 规定长度的替换规定的元素 @Testpublic void test_overlay() {assertEquals("attdd", StringUtils.overlay("aabbccdd", "tt", 1, 6));}// 删除最后一个以 \n \r \t 结尾的字符 @Testpublic void test_chomp() {System.out.println(StringUtils.chomp("\nASDF \n\r\n\r\n asdf\n"));}
}

关于StringUtils的各种方法的功能、解析相关推荐

  1. ipad分屏功能怎么开启_英雄联盟手游设置怎么调最合适?英雄联盟手游设置方法与新手开启功能解析...

    英雄联盟手游设置怎样调最好?很多玩家还不是很了解,下面小编给大家带来英雄联盟手游设置方法与新手开启功能解析,一起来看看吧. 英雄联盟手游设置怎么调最合适?英雄联盟手游设置方法与新手开启功能解析 因为新 ...

  2. Google Test(GTest)使用方法和源码解析——参数自动填充技术分析和应用

    在我们设计测试用例时,我们需要考虑很多场景.每个场景都可能要细致地考虑到到各个参数的选择.比如我们希望使用函数IsPrime检测10000以内字的数字,难道我们要写一万行代码么?(转载请指明出于bre ...

  3. Google Test(GTest)使用方法和源码解析——Listener技术分析和应用

    在<Google Test(GTest)使用方法和源码解析--结果统计机制分析>文中,我么分析了GTest如何对测试结果进行统计的.本文我们将解析其结果输出所使用到的Listener机制. ...

  4. Google Test(GTest)使用方法和源码解析——自动调度机制分析

    在<Google Test(GTest)使用方法和源码解析--概况 >一文中,我们简单介绍了下GTest的使用和特性.从这篇博文开始,我们将深入代码,研究这些特性的实现.(转载请指明出于b ...

  5. 【学习】PCF8563芯片资料和相关功能解析

    [学习]PCF8563芯片资料和相关功能解析 PCF8563芯片是以I2C通讯方式的实时时钟/日历芯片.它提供一个可编程时钟输出,一个中断输出和掉电检测器,所有的地址和数据通过 I2C 总线接口串行传 ...

  6. 用phpcms切换中英文网页的方法(不用解析二级域名)、phpcms完成pc和手机端切换(同一域名)...

    AA.phpcms进行双语切换方法(不用解析二级域名) phpcms进行两种语言的切换,有一把部分的人都是进行的二级域名的解析,这样的话可能会有一部分的麻烦,我这里有一种方法可以不用解析二级域名就可以 ...

  7. uniapp开发获取用户位置信息功能解析

    uniapp开发获取用户位置信息功能解析 问题描述 uniapp相关接口 1.uni.authorize 提前向用户发起授权请求. 接口描述及demo演示 2.uni.getLocation 获取当前 ...

  8. android note分析,随记你的掌上灵感 三星S Note功能解析

    今天要为大家介绍的是三星GALAXY Note升级4.0后的新功能之一的S Note,相信不少已经升级的朋友早已发现了这个程序,不过大家应该还不知如何操作,不要着急,通过今天的介绍和步步演示,你也可以 ...

  9. jQuery方法源码解析--jQuery($)方法(一)

    jQuery方法源码解析--jQuery($)方法 注: 1.本文分析的代码为jQuery.1.11.1版本,在官网上下载未压缩版即可 2.转载请注明出处 jQuery方法: 这个方法大家都不陌生,在 ...

最新文章

  1. C语言实现上三角蛇形矩阵不用数组,C/C++编程笔记:C++ 嵌套循环,含循环打印及蛇形矩阵实例...
  2. Jenkins环境搭建(2)-搭建jmeter+ant+jenkins自动化测试环境
  3. activiti异步执行_对基于消息队列的Activiti异步执行器进行基准测试
  4. 浅谈auto_ptr智能指针
  5. 漫步数学分析番外六(下)
  6. sql%rowcount转mysql_Oracle光速入门二 ——马士兵Oracle视频讲义笔记
  7. iOS环境,Appium不支持driver.current_activity等操作
  8. CSMA协议:改进的ALOHA协议
  9. 轻松解决U盘拷贝文件时提示文件过大问题
  10. 掌控安全Web安全微专业笔记
  11. csr蓝牙驱动Linux,csr harmony蓝牙适配器驱动
  12. Windows server2012R2 企业内部搭建虚拟专用网络服务
  13. : error: control reaches end of non-void function [-Werror=return-type]
  14. 如何利用STM32和迪文串口屏以及WIFI模组进行数据交互
  15. java实现批量注册_Java写的批量域名注册查询程序
  16. 如何在Windows中使用截图工具进行屏幕截图
  17. Ubuntu 18.04 安装 NVIDIA 显卡驱动
  18. 我要写整个中文互联网界最牛逼的JVM系列教程 | 「JVM与Java体系架构」章节:JVM的发展历程
  19. 微信24小时到账_微信转账24小时可撤销吗?延时到账功能可帮忙!
  20. 项目管理中,如何对各种文件进行统一版本管理?

热门文章

  1. JAVA中break和continue用法
  2. 为什么有的人明明能力没问题,却总是抓不住升职加薪的机会?
  3. MySql学习【一】mysql的安装,操作数据库/表/查询表中数据/mysql日期计算
  4. windows 命令行切换目录
  5. java西语_使用Java 8 DateTimeFormatter和西班牙语月份名称进行解析
  6. JJY本地服务器以服务方式运行不能读取消息文件
  7. 二维数组malloc与free
  8. MFC应用的菜单,工具栏和状态栏(vs2019)
  9. Win7输入法图标不见了怎么办
  10. win7语言栏或输入法图标不见了