这里只是我收集一些由apache提供常用会用到的字符串工具类的方法,更多的方法的在下面这个博客中
https://blog.csdn.net/qq_26676207/article/details/72674670
package com;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
public class Strings {
@Test
//assert 断言格式为:assert <boolean表达式>
//如果<boolean表达式>为true,则程序继续执行。
//如果为false,则程序抛出异常,并终止执行。
//不适合在开发环境中使用,但是测试环境下使用不错
public void test1(){
//isEmpty判断这个字符串是否为NUll
assert(StringUtils.isEmpty(null)==true);
//isNotEmpty判断这个字符串是否不为NUll
assert(StringUtils.isNotEmpty(null)==false);
//isBlank判断字符串是否为空 (trim后判断)
assert(StringUtils.isBlank(" ")==true);
//isEmpty判断字符串是否为空 (不trim并判断)
assert(StringUtils.isEmpty(" ")==false);
//equals判断字符串是否相等
assert(StringUtils.equals("a","a")==true);
//trim后为空字符串则转换为null
assert(StringUtils.trimToNull(" ")==null);
//EMPTY返回空字符串
assert(StringUtils.EMPTY=="");
//replace:替换字符串,text第一个参数传字符串,repl第二个传要被替换的字符串,wirh第三个参数传要替换的字符串
assert(StringUtils.replace("Hello123","123","World").equals("HelloWorld"));
//split:分割字符串.str第一个参数为被分割的字符串,separatorChars第二个参数为切割的字符
assert(StringUtils.split("12312", "3")[0].equals("12"));
//join:合并数组为单一字符串,可传分隔符,arr为合并的数组参数,separator第二个参数为分隔符
String [] arr={"1","2","3","4"};
assert(StringUtils.join(arr,"-").equals("1-2-3-4"));
}
}