Java String Split by Comma splitCommand(String command)

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

Description

Split the commandline into separate elements by observing double quotes.

License

Open Source License

Parameter

Parameter Description
command The command in one string. E.g. "perl /usr/bin/pacpl".

Return

A list of single command elements. e.g. {"perl", "/usr/bin/pacpl"}

Declaration

private static List<String> splitCommand(String command) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .  j  a  va  2  s.  c o m*/
   *  Jajuk
   *  Copyright (C) The Jajuk Team
   *  http://jajuk.info
   *
   *  This program is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU General Public License
   *  as published by the Free Software Foundation; either version 2
   *  of the License, or any later version.
   *
   *  This program 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 General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *  
   */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
       * Split the commandline into separate elements by observing double quotes.
       * 
       * @param command The command in one string. E.g. "perl /usr/bin/pacpl".
       * 
       * @return A list of single command elements. e.g. {"perl", "/usr/bin/pacpl"}
       */
    private static List<String> splitCommand(String command) {
        List<String> list = new ArrayList<String>();
        StringBuilder word = new StringBuilder();
        boolean quote = false;
        int i = 0;
        while (i < command.length()) {
            char c = command.charAt(i);
            // word boundary
            if (Character.isWhitespace(c) && !quote) {
                i++;
                // finish current word
                list.add(word.toString());
                word = new StringBuilder();
                // skip more whitespaces
                while (Character.isWhitespace(command.charAt(i)) && i < command.length()) {
                    i++;
                }
            } else {
                // on quote we either start or end a quoted string
                if (c == '"') {
                    quote = !quote;
                }
                word.append(c);
                i++;
            }
        }
        // finish last word
        if (word.length() > 0) {
            list.add(word.toString());
        }
        return list;
    }
}

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)