Java String Split by Delimiter splitUpTo(String s, String delimiter, int cnt)

Here you can find the source of splitUpTo(String s, String delimiter, int cnt)

Description

Split up to a certain number of characters

License

Open Source License

Parameter

Parameter Description
s the string to split
delimiter the delimiter
cnt the max number

Return

the list of split strings

Declaration

public static List<String> splitUpTo(String s, String delimiter, int cnt) 

Method Source Code

//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;
    }
}

Related

  1. splitToken(String str, String delimiter)
  2. splitToList(final String string, final String delim, final int limit)
  3. splitToList(String a_text, String a_delimiter)
  4. splitToList(String s, String delimRegEx)
  5. splitToList(String str, char delimiter)
  6. splitWhileKeepingParentheses(String value, char delimiter)
  7. splitWithEscapeChar(String s, char delimiter, char escape)
  8. stringSplitter(String inStr, String delimiterStr)
  9. tokenizerSplit(String string, String delimiter)