Here you can find the source of splitUpTo(String s, String delimiter, int cnt)
Parameter | Description |
---|---|
s | the string to split |
delimiter | the delimiter |
cnt | the max number |
public static List<String> splitUpTo(String s, String delimiter, int cnt)
//package com.java2s; import java.util.*; public class Main { /**//from w w w . j a va2 s.com * Split up to a certain number of characters * * @param s the string to split * @param delimiter the delimiter * @param cnt the max number * @return the list of split strings */ public static List<String> splitUpTo(String s, String delimiter, int cnt) { List<String> toks = new ArrayList<String>(); for (int i = 0; i < cnt - 1; i++) { int idx = s.indexOf(delimiter); if (idx < 0) { break; } toks.add(s.substring(0, idx)); s = s.substring(idx + 1).trim(); } if (s.length() > 0) { toks.add(s); } return toks; } }