Java编程开发中常涉及到字符串的操作。这里提供了关于String一些操作的包装实现。
1. 获取指定的前缀和后缀之间的内容,返回一个内容列表
/** * 获取指定前缀和后缀之间的内容,并返回一个内容列表 * @param str 操作的目标字符串 * @param c1 前缀字符 * @param c2 后缀字符 * @return 挖出的子串列表 */ public static List<String> dugSubstrings(String str, char c1, char c2) { List<String> result = new ArrayList<String>(); int index1 = -1; int index2 = -1; while ((index1 = str.indexOf(c1, index2 + 1)) != -1 && (index2 = str.indexOf(c2, index1 + 1)) != -1) { result.add(str.substring(index1 + 1, index2)); } return result; } /** * 获取指定前缀和后缀之间的内容,并返回一个内容列表 * @param str 操作的目标字符串 * @param s1 前缀子串 * @param s2 后缀子串 * @return 挖出的子串列表 */ public static List<String> dugSubstrings(String str, String s1, String s2) { List<String> result = new ArrayList<String>(); int index1 = -1; int index2 = -1; while ((index1 = str.indexOf(s1, index2 + 1)) != -1 && (index2 = str.indexOf(s2, index1 + 1)) != -1) { result.add(str.substring(index1 + s1.length(), index2)); } return result; }
单元测试
@Test public void testDugSubstringsWithChar() { String str = "arr[0][1][2]"; System.out.println(StringUtil.dugSubstrings(str, '[', ']')); // 输出 [0, 1, 2] } @Test public void testDugSubstringsWithString() { String str = "map1['key1']=value1&map2['key2']=value2"; System.out.println(StringUtil.dugSubstrings(str, "['", "']")); // 输出 [key1, key2] }
2. 字符串的分隔,支持是否对特定范围内的分隔符进行忽略
/** * 用指定符号将字符串分隔,同时支持忽略指定前缀和后缀之间的分隔符号 * @param str 操作的目标字符串 * @param gap 分隔符 * @param exclude1 前缀 * @param exclude2 后缀 * @return 切分后的子串数组 */ public static String[] splitStringByGap(String str, char gap, String exclude1, String exclude2) { List<String> strs = new ArrayList<String>(); int j = -1; int i = j; j = str.indexOf(gap, i + 1); while (j != -1) { int t = 0; if (str.indexOf(exclude1) < j && (t = str.indexOf(exclude2, j)) > j) { j = str.indexOf(gap, t); } if (j != -1) { strs.add(str.substring(i + 1, j)); i = j; j = str.indexOf(gap, i + 1); } } if (i != -1) { strs.add(str.substring(i + 1)); } return strs.toArray(new String[1]); }
单元测试
@Test public void testSplitStringByGap() { String str = "aa.[[bb.cc]].dd"; List<String> list = Arrays.asList(StringUtil.splitStringByGap(str, '.', "[[", "]]")); assertEquals(3, list.size()); System.out.println(list); }
3. 字符串去除所有空白和换行
/** * 将字符串中的转义符去掉 * @param str 操作的目标字符串 * @return 瘦身后的字符串 */ public static String replaceBlank(String str) { String dest = ""; if (str != null) { Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(str); dest = m.replaceAll(""); } return dest; }
单元测试
@Test public void testReplaceBlank() { String str = "asdsa\n\tasdsa \nsadsa\tsadsa\nsadsa"; System.out.println(str); System.out.println(StringUtil.replaceBlank(str)); }
4. 通过反射将字符串转成基本数据类型
用于在事先不知道最终需要转换为什么类型的情况下,在通过反射编写工具类时可以用到。
@SuppressWarnings("unchecked") /** * 通过反射将字符串转换为基本数据类型 * @param str 需要转换的字符串 * @param clz 转换后的类型 * @return 转换后的值 * @throws Exception */ public static <T> T transferString2PrimitiveType(String str, Class<T> clz) throws Exception { T obj = null; try { if (Boolean.class == clz || boolean.class == clz) { obj = (T) Boolean.valueOf(str); } else if (Character.class == clz || char.class == clz) { obj = (T) (Character) str.toCharArray()[0]; } else if (Short.class == clz || short.class == clz) { obj = (T) Short.valueOf(str); } else if (Integer.class == clz || int.class == clz) { obj = (T) Integer.valueOf(str); } else if (Byte.class == clz || byte.class == clz) { obj = (T) Byte.valueOf(str); } else if (Long.class == clz || long.class == clz) { obj = (T) Long.valueOf(str); } else if (Double.class == clz || double.class == clz) { obj = (T) Double.valueOf(str); } else if (Float.class == clz || float.class == clz) { obj = (T) Float.valueOf(str); } } catch (Exception e) { throw new Exception(str + "无法转换为" + clz.getSimpleName() + "类型"); } return obj; }
单元测试
@Test public void testTransferString2PrimitiveType() throws Exception{ String str = "true"; System.out.println(StringUtil.transferString2PrimitiveType(str, Boolean.class)); str = "12.3f"; System.out.println(StringUtil.transferString2PrimitiveType(str, Float.class)); str = "123.44"; System.out.println(StringUtil.transferString2PrimitiveType(str, Double.class)); }
相关推荐
java 字符串截取工具类java 字符串截取工具类
Java中字符串生成图片工具类 将字符串转换为BufferedImage对象 将字符串转换为BufferedImage对象字段--自动换行
java压缩字符串
字符串工具类,格式字符串、随机数生成、ip区间判断!
java 字符串 操作 源码 工具
不需要关心接受的字符串编码是UTF_8还是GBK,还是ios-8859-1,自动转换为utf-8编码格式,无需判断字符串原有编码,用法://处理编码String newStr = GetEncode.transcode(oldStr);
java字符串处理的util工具类,也可以同时添加一些其他字符处理的方法,对于项目中特殊字符处理,完成项目中基本的字串符处理。
使用java代码实现搜索遍历文本文件并替换文本内容, 支持脚本bat和sh调用.
今天小编就为大家分享一篇关于java字符串中${}或者{}等的占位符替换工具类,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
- commons-beanutils-1.8.0.jar - commons-collections-3.1.jar - commons-lang-2.5.jar - commons-logging-1.2.jar - ezmorph-1.0.6.jar - json-lib-2.4-jdk15.jar
里面有三个类。时间的常用工具类,字符串常用工具类,网络获取时间类。
1.删除字符 2.去空格 3.去除所有空格和特殊字符 4.判断邮箱 5.字符串集合转字符串 6.只保留汉字 7.关键词前后是否包含英文字母 8.去除字符串中的附件 9.获取下标单个位置和所有位置
java 常用字符串处理工具类! java 常用字符串处理工具类!
java中根据汉字字符串获取拼音首字母工具类
字符转换工具类:包含了生成html最全的源码,并有详细的注释
String 字符串操作工具类,sql防注入方法 过滤通过页面表单提交的字符 用特殊的字符连接字符串 将字符串数组转换为逗号链接的字符串,并且去掉最后一个逗号 分割字符串 字符串字符集转换 编码转换方法 将html文档...
java工具类去掉字符串String中的.点。android开发java程序员常用工具类。博文介绍:http://blog.csdn.net/qq_21376985/article/details/51482313
Java常用的字符串操作工具 字符串转为整型,如转换出异常将返回0 补充字符系列 -1:左补充; 1:右补充
对网上问的一些问题的总结,封装成了一个工具类。完成的功能有: 1、判断字符串中是否有中文; 2、得到字符串中有几个中文; 3、判断字符串中有没有连续的几个中文;
JAVA字符串操作类CTool.java字符转换类,此类中收集Java编程中WEB开发常用到的一些工具。为避免生成此类的实例,构造方法被申明为private类型的。封装的功能:字符串从GBK编码转换为Unicode编码、对字符串进行md5...