赞
踩
/** * Splits the provided text into an array, separator specified, preserving * all tokens, including empty tokens created by adjacent separators. * * CSVUtil.split(null, *, true) = null * CSVUtil.split("", *, , true) = [] * CSVUtil.split("a.b.c", '.', true) = ["a", "b", "c"] * CSVUtil.split("a...c", '.', true) = ["a", "", "", "c"] * CSVUtil.split("a...c", '.', false) = ["a", "c"] * * @param str * the string to parse * @param separatorChar * the seperator char * @param preserveAllTokens * if true, adjacent separators are treated as empty token * separators * @return the splitted string */ public static String[] split(String str, char separatorChar, boolean preserveAllTokens) { if (str == null) { return null; } int len = str.length(); if (len == 0) { return new String[0]; } List<String> list = new ArrayList<String>(); int i = 0, start = 0; boolean match = false; boolean lastMatch = false; while (i < len) { if (str.charAt(i) == separatorChar) { if (match || preserveAllTokens) { list.add(str.substring(start, i)); match = false; lastMatch = true; } 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()]); }
while (i < len) { if (str.charAt(i) == separatorChar) { if (match) { list.add(str.substring(start, i));lastMatch = true; match = false; } start = ++i; continue; }lastMatch = false; match = true; i++; }
if (match || preserveAllTokens && lastMatch) { list.add(str.substring(start, i)); }
String[] cc = "a,b,c,,d,e,f".split(",");
String[] cc = "a,b,c,,d,e,f".split(",",7);
* <p> The <tt>limit</tt> parameter controls the number of times the * pattern is applied and therefore affects the length of the resulting * array. If the limit <i>n</i> is greater than zero then the pattern * will be applied at most <i>n</i> - 1 times, the array's * length will be no greater than <i>n</i>, and the array's last entry * will contain all input beyond the last matched delimiter. If <i>n</i> * is non-positive then the pattern will be applied as many times as * possible and the array can have any length. If <i>n</i> is zero then * the pattern will be applied as many times as possible, the array can * have any length, and trailing empty strings will be discarded.
String[] dd = "a,,,,,,d,e,".split(","); System.out.println(Arrays.toString(dd)+":"+dd.length); String[] ee = "a,,,,,,d,e,".split(",",8); System.out.println(Arrays.toString(ee)+":"+ee.length); String[] ff = "a,,,,,,d,e,".split(",",10); System.out.println(Arrays.toString(ff)+":"+ff.length);
[a, , , , , , d, e]:8[a, , , , , , d, e,]:8[a, , , , , , d, e, ]:9
.String[] aa = CSVUtil.split("a,,,,,,d,e,", ',', false); System.out.println(Arrays.toString(aa)+":"+aa.length); String[] bb = CSVUtil.split("a,,,,,,d,e,", ',', true); System.out.println(Arrays.toString(bb)+":"+bb.length);
[a, d, e]:3[a, , , , , , d, e, ]:9
String[] hh = null; String input = "abcddaeaaa"; int limit = 100; int index = 0; boolean matchLimited = limit > 0; ArrayList<String> matchList = new ArrayList<String>(); Pattern pattern = Pattern.compile("A", 2); Matcher m = pattern.matcher(input); while(m.find()) { if (!matchLimited || matchList.size() < limit - 1) { String match = input.subSequence(index, m.start()).toString(); matchList.add(match); index = m.end(); } else if (matchList.size() == limit - 1) { // last one String match = input.subSequence(index, input.length()).toString(); matchList.add(match); index = m.end(); } } // If no match was found, return this if (index == 0) hh= new String[] {input.toString()}; // Add remaining segment if (!matchLimited || matchList.size() < limit) matchList.add(input.subSequence(index, input.length()).toString()); // Construct result int resultSize = matchList.size(); if (limit == 0){ while (resultSize > 0 && matchList.get(resultSize-1).equals("")){ resultSize--; } } String[] result = new String[resultSize]; hh= matchList.subList(0, resultSize).toArray(result); //if limit is 0, then String.split at this step is delete the last "". // so we should be change limit to s.length.
String input1 = "a,,,,,,d,e,";String[] ii = input1.split(",",input1.length()); System.out.println(Arrays.toString(ii)+":"+ii.length);
[a, , , , , , d, e, ]:9
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。