Java String Split split(String input)

Here you can find the source of split(String input)

Description

split

License

Open Source License

Declaration

public static String[] split(String input) 

Method Source Code

//package com.java2s;
/**//ww w. ja  va 2  s  .  co  m
 *    Created on Oct 21, 2010
 *    This file is part of JObexFTP 2.0, and it contains parts of OBEX4J.
 *
 *    JObexFTP is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    JObexFTP is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with JObexFTP.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.util.ArrayList;

public class Main {
    public static String[] split(String input) {
        if (!input.contains("\"")) {
            return input.split(" ");
        }
        ArrayList<String> l = new ArrayList<String>();
        boolean isInQuotes = false;
        String stack = "";
        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) == '\"') {
                if (isInQuotes) {
                    if (stack.length() > 0) {
                        l.add(stack);
                        stack = "";
                    }
                    isInQuotes = false;
                } else {
                    isInQuotes = true;
                }
            } else if (input.charAt(i) == ' ') {
                if (isInQuotes) {
                    stack += " ";
                } else {
                    if (stack.length() > 0) {
                        l.add(stack);
                        stack = "";
                    }
                }
            } else {
                stack += input.charAt(i);
            }
        }
        if (stack.length() > 0) {
            l.add(stack);
            stack = "";
        }
        return l.toArray(new String[l.size()]);
    }
}

Related

  1. split(String cmd)
  2. split(String content)
  3. Split(String content, String sub_seq)
  4. split(String input)
  5. split(String input)
  6. split(String s)
  7. split(String s)
  8. split(String s)
  9. split(String s)