Java String Split by Length splitParams(String string, int max)

Here you can find the source of splitParams(String string, int max)

Description

split Params

License

Open Source License

Declaration

public static String[] splitParams(String string, int max) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    public static String[] splitParams(String string, int max) {
        String[] words = string.trim().split(" ");
        if (words.length <= 1) {
            return words;
        }/*from w  w  w.  j  a  v  a2  s  .  c o m*/
        ArrayList<String> list = new ArrayList<String>();
        char quote = ' ';
        String building = "";

        for (String word : words) {
            if (word.length() == 0)
                continue;
            if (max > 0 && list.size() == max - 1) {
                if (!building.isEmpty())
                    building += " ";
                building += word;
            } else if (quote == ' ') {
                if (word.length() == 1 || (word.charAt(0) != '"' && word.charAt(0) != '\'')) {
                    list.add(word);
                } else {
                    quote = word.charAt(0);
                    if (quote == word.charAt(word.length() - 1)) {
                        quote = ' ';
                        list.add(word.substring(1, word.length() - 1));
                    } else {
                        building = word.substring(1);
                    }
                }
            } else {
                if (word.charAt(word.length() - 1) == quote) {
                    list.add(building + " " + word.substring(0, word.length() - 1));
                    building = "";
                    quote = ' ';
                } else {
                    building += " " + word;
                }
            }
        }
        if (!building.isEmpty()) {
            list.add(building);
        }
        return list.toArray(new String[list.size()]);
    }

    public static String[] splitParams(String string) {
        return splitParams(string, 0);
    }

    public static String[] splitParams(String[] split, int max) {
        return splitParams(arrayJoin(split, ' '), max);
    }

    public static String[] splitParams(String[] split) {
        return splitParams(arrayJoin(split, ' '), 0);
    }

    public static String arrayJoin(String[] array, char with) {
        if (array == null || array.length == 0) {
            return "";
        }
        int len = array.length;
        StringBuilder sb = new StringBuilder(16 + len * 8);
        sb.append(array[0]);
        for (int i = 1; i < len; i++) {
            sb.append(with);
            sb.append(array[i]);
        }
        return sb.toString();
    }
}

Related

  1. splitFilenames(String text)
  2. splitHtmlString(String stringToSplit, int maxLength)
  3. splitIntoFrames(String src, int maxFrameSize)
  4. splitIntoLine(String input, int maxCharInLine)
  5. splitLines(String paragraph, int lineLength)
  6. splitPreserveAllTokens(String str, String separatorChars, int max)
  7. splitString(String orig, String delim, int max)
  8. splitString(String s, int maxSize)
  9. splitString(String str, int length)