首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

jodd应用示例

2012-12-18 
jodd使用示例package org.xiaochen.test.joddimport org.junit.Testimport jodd.util.StringUtil/** *

jodd使用示例

package org.xiaochen.test.jodd;import org.junit.Test;import jodd.util.StringUtil;/** * String字符串的操作工具类,太强大以至于我要发疯 *  * @author DJZHOU *  */public class StringExamUtil{@Testpublic void stringExam(){String exam = "abcdefg10101010abcdefg";String result = "";/* * replace 字符替换 */// 将字符串exam中的a替换成bresult = StringUtil.replace(exam, "a", "b");// 将字符串exam中的a替换成8,b替换成9result = 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替换成bresult = StringUtil.replaceFirst(exam, "a", "b");// 将字符串exam中的第一个a替换成b 这里是替换字符result = StringUtil.replaceFirst(exam, 'a', 'b');// 将字符串exam中的最后一个a替换成bresult = 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索引的字符是不是dSystem.out.println(StringUtil.isCharAtEqual(exam, 4, 'd'));// 对比字符串exam中的第4索引的字符是不是 不是dSystem.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为开头,则去掉aresult = StringUtil.stripLeadingChar(exam, 'a');// 若这个字符串以g为结尾,则去掉gresult = StringUtil.stripTrailingChar(exam, 'g');// 若该字符串为"" 则返回null 若不是则返回字符串result = StringUtil.crop("");// 裁剪数组 将""变成nullStringUtil.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字符开始切割字符串 保留fresult = StringUtil.cutFromIndexOf(exam, 'f');// 从字符串的fg字符串开始切割字符串 保留fgresult = StringUtil.cutFromIndexOf(exam, "fg");// 检查字符串是否为abc开头,若为此开头,则切割掉abcresult = StringUtil.cutPrefix(exam, "abc");// 检查字符串是否为efg结尾,若为此结尾,则切割掉efgresult = StringUtil.cutSuffix(exam, "efg");// 检查字符串是否为efg开头或结尾,若为此开头或结尾,则切割掉efgresult = StringUtil.cutSurrounding(exam, "efg");// 检查字符串是否为abc开头efg结尾,若为为abc开头efg结尾,则切割掉result = StringUtil.cutSurrounding(exam, "abc", "efg");// 截取到字符串的f字符开始切割字符串 不保留fresult = StringUtil.cutToIndexOf(exam, 'f');// 截取到字符串的fg字符串开始切割字符串 不保留fgresult = StringUtil.cutToIndexOf(exam, "fg");/* * 其他很多小巧的方法,可以自行研究 */System.out.println(result);}}


热点排行