java jodd_java jodd框架介绍及使用示例

import org.junit.Test;

import jodd.util.StringUtil;

/**

* String字符串的操作工具类,太强大以至于我要发疯

*

* @author DJZHOU

*

*/

public class StringExamUtil

{

@Test

public void stringExam()

{

String exam = "abcdefg10101010abcdefg";

String result = "";

/*

* replace 字符替换

*/

// 将字符串exam中的a替换成b

result = StringUtil.replace(exam, "a", "b");

// 将字符串exam中的a替换成8,b替换成9

result = StringUtil.replace(exam, new String[] { "a", "b" }, new String[] { "8", "9" });

// 将字符串exam中的a替换成b 这里是替换字符

result = StringUtil.replaceChar(exam, 'a', 'b');

// 将字符串exam中的a替换成8,b替换成9 这里是替换字符

result = StringUtil.replaceChars(exam, new char[] { 'a', 'b' }, new char[] { '8', '9' });

// 将字符串exam中的第一个a替换成b

result = StringUtil.replaceFirst(exam, "a", "b");

// 将字符串exam中的第一个a替换成b 这里是替换字符

result = StringUtil.replaceFirst(exam, 'a', 'b');

// 将字符串exam中的最后一个a替换成b

result = StringUtil.replaceLast(exam, "a", "b");

// 将字符串exam中的最后一个a替换成b 这里是替换字符

result = StringUtil.replaceLast(exam, 'a', 'b');

// 将字符串exam中的a和A替换成FF b和B替换成gg 即忽略大小写

result = StringUtil.replaceIgnoreCase(exam, new String[] { "a", "b" }, new String[] { "FF", "gg" });

/*

* remove 字符移除

*/

// 将字符串exam中的a移除

result = StringUtil.remove(exam, "a");

// 将字符串exam中的a移除 移除的是字符

result = StringUtil.remove(exam, 'a');

// 将字符串exam中的a b移除 移除的是字符 最后一个参数为无限参数

result = StringUtil.removeChars(exam, 'a', 'b');

// 将字符串exam中的a移除

result = StringUtil.removeChars(exam, "a");

/*

* 判断字符串是否为空

*/

// 判断字符串exam是否为空

System.out.println(StringUtil.isEmpty(exam));

// 判断字符串exam是否不为空

System.out.println(StringUtil.isNotEmpty(exam));

// 判断字符串exam是否为空 这里的空为去掉空格之后

System.out.println(StringUtil.isBlank("   "));

// 判断字符串exam是否不为空 这里的空为去掉空格之后

System.out.println(StringUtil.isNotBlank("   "));

// 判断字符串(无限参数)是否都为空 他们之间的关系为并且

System.out.println(StringUtil.isAllEmpty(exam, "  ", "null"));

// 判断字符串(无限参数)是否都为空 这里的空为去掉空格之后 他们之间的关系为并且

System.out.println(StringUtil.isAllBlank(exam, "  ", "null"));

// 对比字符串exam中的第4索引的字符是不是d

System.out.println(StringUtil.isCharAtEqual(exam, 4, 'd'));

// 对比字符串exam中的第4索引的字符是不是 不是d

System.out.println(StringUtil.isCharAtEscaped(exam, 4, 'd'));

/*

* equals安全的字符串对比是否相等 不需要考虑null.equals等问题

*/

// 判断字符串exam与aaa是否相等

System.out.println(StringUtil.equals(exam, "aaa"));

// 判断两个数组是否相等

System.out.println(StringUtil.equals(new String[] { "aaa" }, new String[] { "aaa", "bbb" }));

// 判断两个数组是否相等 且忽略大小写

System.out.println(StringUtil.equalsIgnoreCase(new String[] { "aaa" }, new String[] { "aaa", "bbb" }));

// 获取字符串bbb在数组中的索引

System.out.println(StringUtil.equalsOne("bbb", new String[] { "aaa", "bbb" }));

// 获取字符串bbb在数组中的索引 且忽略大小写

System.out.println(StringUtil.equalsOneIgnoreCase("bbb", new String[] { "aaa", "bbb" }));

/*

* 首字母的更改

*/

// 首字母大写

result = StringUtil.capitalize(exam);

// 首字母小写

result = StringUtil.uncapitalize(exam);

/*

* split字符串分割

*/

// 将字符串按 , 分割

String[] array = StringUtil.split("1,2,3,4,5,6,7,8", ",");

/*

* indexOf 获取字符串中的字符索引

*/

/*

* Strips, crops, trims and cuts

*/

// 若这个字符串以a为开头,则去掉a

result = StringUtil.stripLeadingChar(exam, 'a');

// 若这个字符串以g为结尾,则去掉g

result = StringUtil.stripTrailingChar(exam, 'g');

// 若该字符串为"" 则返回null 若不是则返回字符串

result = StringUtil.crop("");

// 裁剪数组 将""变成null

StringUtil.cropAll(new String[] { "", " " });

// 去掉字符串两边的空格

result = StringUtil.trimDown("  aa  ");

// 去掉字符串左边的空格

result = StringUtil.trimLeft("  aa  ");

// 去掉字符串右边的空格

result = StringUtil.trimRight("  aa  ");

// 去掉字符串右边的空格

String[] array2 = new String[] { "  aa  ", "  b  b" };

/*

* 去掉数组内空格

*/

StringUtil.trimAll(array2);

StringUtil.trimDownAll(array2);

for (String string : array2)

{

System.out.println(string);

}

/*

* 切割字符串

*/

// 从字符串的f字符开始切割字符串 保留f

result = StringUtil.cutFromIndexOf(exam, 'f');

// 从字符串的fg字符串开始切割字符串 保留fg

result = StringUtil.cutFromIndexOf(exam, "fg");

// 检查字符串是否为abc开头,若为此开头,则切割掉abc

result = StringUtil.cutPrefix(exam, "abc");

// 检查字符串是否为efg结尾,若为此结尾,则切割掉efg

result = StringUtil.cutSuffix(exam, "efg");

// 检查字符串是否为efg开头或结尾,若为此开头或结尾,则切割掉efg

result = StringUtil.cutSurrounding(exam, "efg");

// 检查字符串是否为abc开头efg结尾,若为为abc开头efg结尾,则切割掉

result = StringUtil.cutSurrounding(exam, "abc", "efg");

// 截取到字符串的f字符开始切割字符串 不保留f

result = StringUtil.cutToIndexOf(exam, 'f');

// 截取到字符串的fg字符串开始切割字符串 不保留fg

result = StringUtil.cutToIndexOf(exam, "fg");

/*

* 其他很多小巧的方法,可以自行研究

*/

System.out.println(result);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值