JAVA Split方法,拆分字符串后,去除返回的空值
来源:互联网 发布:淘宝客服介入电话 编辑:程序博客网 时间:2024/05/21 15:00
1.使用org.apache.commons.lang3.StringUtils中的StringUtils.split方法
String strno = ",1,1,2"; for (String string : StringUtils.split(strno, ",")) { System.out.println(string); }
2.StringUtils的具体代码
private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) { // Performance tuned for 2.0 (JDK1.4) // Direct code is quicker than StringTokenizer. // Also, StringTokenizer uses isSpace() not isWhitespace() if (str == null) { return null; } int len = str.length(); if (len == 0) { return ArrayUtils.EMPTY_STRING_ARRAY; } List<String> list = new ArrayList<String>(); int sizePlus1 = 1; int i = 0, start = 0; boolean match = false; boolean lastMatch = false; if (separatorChars == null) { // Null separator means use whitespace while (i < len) { if (Character.isWhitespace(str.charAt(i))) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } else if (separatorChars.length() == 1) { // Optimise 1 character case char sep = separatorChars.charAt(0); while (i < len) { if (str.charAt(i) == sep) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } else { // standard case while (i < len) { if (separatorChars.indexOf(str.charAt(i)) >= 0) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } lastMatch = false; match = true; i++; } } if (match || preserveAllTokens && lastMatch) { list.add(str.substring(start, i)); } return list.toArray(new String[list.size()]); }
public static String[] split(String str, String separatorChars) { return splitWorker(str, separatorChars, -1, false); }
1 0
- JAVA Split方法,拆分字符串后,去除返回的空值
- 去除Split分割后的空字符串
- 去除Split分割后的空字符串
- 字符串调用split以点逗"."符号作为拆分符时返回的是空数组
- 关于split拆分后最后获取不到空字符串的问题
- split()方法字符串拆分
- split方法拆分字符串
- Android中字符串的拆分split方法
- Android中字符串的拆分---split()方法
- 字符串的拆分split
- Java split拆分使用竖线为分隔符的字符串方法
- Java split拆分使用竖线为分隔符的字符串方法
- Java--split拆分使用竖线为分隔符的字符串方法
- .net Split方法字符串根据字符串拆分
- 一个扩展后的Split方法,可根据字符串分隔字符,返回字符数组
- sqlserver字符串拆分(split)方法汇总
- sqlserver字符串拆分(split)方法汇总
- sqlserver字符串拆分(split)方法汇总
- 军事物联网如何改变未来战争模式?
- Spark机器学习库(MLlib)官方指南手册中文版
- java中JVM的内存分配
- yii2出现An internal server error occurred.
- Apple pay
- JAVA Split方法,拆分字符串后,去除返回的空值
- 微信JSSDK chooseImage uploadImage 接口 在IOS与安卓的差异
- JavaScript中的 == 与 ===
- p3615_floyd
- 走迷宫(存一下而已)
- Leetcode学习(30)—— Distribute Candies
- java中应用Stack进行算术运算
- Source Insight用法
- 【JAVA面试题系列一】面试题总汇--JAVA基础部分