Java String Split splitArguments(String string)

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

Description

Split command line into arguments.

License

Open Source License

Declaration

public static String[] splitArguments(String string) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    /** Split command line into arguments.
    Allows " for words containing whitespaces. */
    public static String[] splitArguments(String string) {
        assert string != null;
        ArrayList<String> result = new ArrayList<String>();
        boolean escape = false;
        boolean inString = false;
        StringBuilder token = new StringBuilder();
        for (int i = 0; i < string.length(); ++i) {
            char c = string.charAt(i);
            if (c == '"' && !escape) {
                if (inString) {
                    result.add(token.toString());
                    token.setLength(0);/*from ww  w . j ava  2s  . c o  m*/
                }
                inString = !inString;
            } else if (Character.isWhitespace(c) && !inString) {
                if (token.length() > 0) {
                    result.add(token.toString());
                    token.setLength(0);
                }
            } else
                token.append(c);
            escape = (c == '\\' && !escape);
        }
        if (token.length() > 0)
            result.add(token.toString());
        return result.toArray(new String[result.size()]);
    }
}

Related

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