Java String Split splitArguments(String string)

Here you can find the source of splitArguments(String string)

Description

Splits specified string in a manner which is similar to what is done for command line arguments.

License

Open Source License

Declaration

public static String[] splitArguments(String string) 

Method Source Code


//package com.java2s;

import java.util.ArrayList;

public class Main {
    /**/*from www  .  j  a va2 s  . co  m*/
     * Splits specified string in a manner which is similar to what is done
     * for command line arguments. Returns the list of ``command line
     * arguments''.
     * <p>Example: returns <code>{"one", "two", "three", " \"four\" ", "
     * 'five' "}</code> for input string:
     * <pre>one   "two"    'three'   " \"four\" "   " 'five' "</pre>
     * <p>Note that escaped sequences such as "\n" and "\t" contained in
     * specified string are left as is. The reason for this is that specified
     * string may contain any whitespace character including '\n' and '\t',
     * provided that these characters are contained in a quoted argument.
     */
    public static String[] splitArguments(String string) {
        ArrayList<String> list = new ArrayList<String>();
        char quote = 0;
        StringBuilder arg = null;

        final int length = string.length();
        for (int i = 0; i < length; ++i) {
            char c = string.charAt(i);

            if (Character.isWhitespace(c)) {
                if (quote != 0) {
                    arg.append(c);
                } else {
                    if (arg != null) {
                        list.add(arg.toString());
                        arg = null;
                    }
                }
            } else {
                if (arg == null) {
                    arg = new StringBuilder();

                    switch (c) {
                    case '\"':
                    case '\'':
                        quote = c;
                        break;
                    default:
                        arg.append(c);
                    }
                } else {
                    if (c == quote) {
                        int last = arg.length() - 1;
                        if (last >= 0 && arg.charAt(last) == '\\') {
                            arg.setCharAt(last, quote);
                        } else {
                            list.add(arg.toString());
                            arg = null;
                            quote = 0;
                        }
                    } else {
                        arg.append(c);
                    }
                }
            }
        }

        if (arg != null) {
            list.add(arg.toString());
        }

        String[] args = new String[list.size()];
        return list.toArray(args);
    }
}

Related

  1. splitArguments(final String str)
  2. splitArguments(String arguments)
  3. splitArguments(String s)
  4. splitArguments(String str)
  5. splitArguments(String string)
  6. splitArray(byte[] src, int size)
  7. splitArray(String[] srcArr, int start, int end)
  8. splitAt(final String input, final String seperator)
  9. splitAtLastBlank(String s, int width)