Java String Split splitArguments(String s)

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

Description

split Arguments

License

Open Source License

Declaration

public static String[] splitArguments(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 java2script.org and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   www  . j  a v a 2s .c o  m
 *     Zhou Renjian - initial API and implementation
 *******************************************************************************/

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

public class Main {
    public static String[] splitArguments(String s) {
        if (s == null || s.trim().length() == 0) {
            return new String[0];
        }
        s = s.trim();
        List list = new ArrayList();
        int lastIndex = 0;
        int index = 0;
        int length = s.length();
        while (index < length) {
            StringBuffer buffer = new StringBuffer();
            boolean isInString = false;
            boolean lastInStringConcat = false;
            char lastChar = 0;
            while (index < length) {
                char ch = s.charAt(index++);
                if (!isInString) {
                    if (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t') {
                        lastChar = ch;
                        if (index == lastIndex + 1) {
                            lastIndex = index;
                            continue;
                        }
                        break;
                    } else if (ch == '\"') {
                        if (lastChar != '\\') {
                            isInString = true;
                            lastChar = ch;
                            lastInStringConcat = index != lastIndex + 1;
                            continue;
                        } else {
                            buffer.deleteCharAt(buffer.length() - 1);
                            //lastInStringConcat = index != lastIndex + 1;
                        }
                    }
                    lastChar = ch;
                } else {
                    if (ch == '\"') {
                        if (lastChar != '\\') {
                            isInString = false;
                            //break;
                            if (lastInStringConcat) {
                                continue;
                            } else {
                                break;
                            }
                        } else {
                            buffer.deleteCharAt(buffer.length() - 1);
                        }
                        //buffer.append('\"');
                    }
                }
                buffer.append(ch);
                lastChar = ch;
            }
            lastIndex = index;
            if (buffer.length() != 0) {
                list.add(buffer.toString());
            }
        }
        return (String[]) list.toArray(new String[0]);
    }
}

Related

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