Java String Split by Comma splitCommand(String command)

Here you can find the source of splitCommand(String command)

Description

split Command

License

Open Source License

Declaration

public static String[] splitCommand(String command) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] splitCommand(String command) {
        List<String> elements = new ArrayList<String>();
        String[] base = command.split(" ");

        int realIndex = 0;
        boolean inString = false;
        for (int i = 0; i < base.length; i++) {
            for (int j = 0; j < base[i].length(); j++) {

                if (base[i].charAt(j) == '"') {
                    if (j == 0)
                        inString = !inString;
                    else if (base[i].charAt(j - 1) != '\\')
                        inString = !inString;
                }//  w  w w  .java  2  s.co m
            }
            if (elements.size() <= realIndex)
                elements.add(base[i]);
            else
                elements.set(realIndex, elements.get(realIndex) + " " + base[i]);
            if (!inString)
                realIndex++;
        }

        return elements.toArray(new String[0]);
    }
}

Related

  1. splitAtCommaOrPipe(String input)
  2. splitComma(final String strWithComma)
  3. splitComma(String configValue)
  4. splitCommand(String command)
  5. splitCommand(String command)
  6. splitCommandString(String command_string)
  7. splitCommaSequence(String argumentString)
  8. splitCommaStr(String commaStr)