Java String Split splitArguments(String str)

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

Description

split Arguments

License

LGPL

Declaration

public static List<String> splitArguments(String str) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> splitArguments(String str) {
        List<String> list = new ArrayList<>();
        splitArguments(str, list);/*ww w. j  a  v a2  s.  c  om*/
        return list;
    }

    public static void splitArguments(String str, List<String> list) {
        StringBuilder sb = new StringBuilder();
        int pos = 0;
        boolean escape = false, inQuotes = false;
        while (pos < str.length()) {
            char ch = str.charAt(pos++);
            if (escape) {
                escape = false;
                sb.append(ch);
            } else if (ch == '\\') {
                escape = true;
            } else if (ch == '"' || ch == '\'') {
                if (inQuotes) {
                    inQuotes = false;
                    list.add(sb.toString());
                    sb.setLength(0);
                } else {
                    inQuotes = true;
                }
            } else if (ch == ' ' && !inQuotes) {
                if (sb.length() > 0) {
                    list.add(sb.toString());
                    sb.setLength(0);
                }
            } else {
                sb.append(ch);
            }
        }
        if (sb.length() > 0) {
            list.add(sb.toString());
        }
    }
}

Related

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