Java String Split by Comma splitCommandString(String command_string)

Here you can find the source of splitCommandString(String command_string)

Description

split Command String

License

Open Source License

Declaration

private static ArrayList<String> splitCommandString(String command_string) 

Method Source Code

//package com.java2s;
/* Description and License
 * A Java library that wraps the functionality of the native image 
 * processing library OpenCV/*from w w w . j a  v a2  s  .com*/
 *
 * (c) Sigurdur Orn Adalgeirsson (siggi@alum.mit.edu)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */

import java.util.ArrayList;

public class Main {
    private static ArrayList<String> splitCommandString(String command_string) {
        command_string += " "; // For parantheses spitting

        /*
         * First we consider \"
         */
        String[] substrings = command_string.split("\"");
        if (substrings.length % 2 == 0) {
            throw new RuntimeException("Unmatched parantheses in: " + command_string);
        }

        ArrayList<String> output = new ArrayList<String>();
        for (int i = 0; i < substrings.length; i++) {

            // If this substring was inside parentheses
            if ((i % 2) != 0) {
                output.add(substrings[i]);
            }
            // Otherwise we just split them up
            else {
                String[] subsubstrings = substrings[i].trim().split(" ");
                for (String string : subsubstrings) {
                    String string2 = string.trim();
                    if (string2.length() > 0) {
                        output.add(string2);
                    }
                }
            }
        }

        //      System.out.println("Parsing command string: "+command_string);
        //      for (String string : output) {
        //         System.out.println("Subcommand: "+string);
        //      }

        return output;
    }
}

Related

  1. splitComma(final String strWithComma)
  2. splitComma(String configValue)
  3. splitCommand(String command)
  4. splitCommand(String command)
  5. splitCommand(String command)
  6. splitCommaSequence(String argumentString)
  7. splitCommaStr(String commaStr)
  8. splitDoubleParentCommas(String input)
  9. splitOnComma(String cs)