Java String Split splitArguments(final String str)

Here you can find the source of splitArguments(final String str)

Description

split Arguments

License

Open Source License

Declaration

public static String[] splitArguments(final String str) 

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 final char NULL_CHAR = '\0';
    @SuppressWarnings("HardcodedFileSeparator")
    public static final char ESCAPE_CHAR = '\\';
    public static final char SPACE = ' ';
    public static final char QUOTE = '"';
    public static final char APOSTROPHE = '\'';

    public static String[] splitArguments(final String str) {
        final char[] charArray = str.toCharArray();
        final List<String> args = new ArrayList<>(10);
        StringBuilder builder = new StringBuilder(32);
        boolean isBuildingString = false;
        char buildingChar = NULL_CHAR;
        for (int i = 0, charArrayLength = charArray.length; i < charArrayLength; i++) {
            final char c = charArray[i];
            switch (c) {
            case SPACE:
                if (!isBuildingString
                        && ((i == 0) || (charArray[i - 1] != ESCAPE_CHAR))) {
                    args.add(builder.toString());
                    builder = new StringBuilder(32);
                    break;
                }/*from  w  w  w  .  j av  a2 s .c  o m*/
                builder.append(c);
                break;
            case APOSTROPHE:
            case QUOTE:
                if ((i == 0) || (charArray[i - 1] != ESCAPE_CHAR)) {
                    if (!isBuildingString) {
                        isBuildingString = true;
                        buildingChar = c;
                        break;
                    }
                    if (c == buildingChar) {
                        isBuildingString = false;
                        buildingChar = NULL_CHAR;
                        args.add(builder.toString());
                        builder = new StringBuilder(32);
                        while (++i < charArrayLength) {
                            if (charArray[i] == SPACE) {
                                break;
                            }
                        }
                        break;
                    }
                }
            default:
                builder.append(c);
                break;
            case ESCAPE_CHAR:
                break;
            }
        }
        if (builder.length() > 0) {
            args.add(builder.toString());
        }
        return args.toArray(new String[args.size()]);
    }
}

Related

  1. split_str(String input, String split)
  2. splitAndTrim(String str)
  3. splitAndTrim(String str, String splitBy)
  4. splitAndUnEscape(String str)
  5. splitArgs(String args, int expectedArgs)
  6. splitArguments(String arguments)
  7. splitArguments(String s)
  8. splitArguments(String str)
  9. splitArguments(String string)