Java String Split by Quote splitStringWithQuotes(String in)

Here you can find the source of splitStringWithQuotes(String in)

Description

split String With Quotes

License

Open Source License

Declaration

public static String[] splitStringWithQuotes(String in) 

Method Source Code

//package com.java2s;
/**/*from  ww w . j  a  v a2 s  .  c o m*/
 * Title:        NoUnit - Identify Classes that are not being unit Tested
 *
 * Copyright (C) 2001  Paul Browne , FirstPartners.net
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @author Paul Browne
 * @version 0.6
 *
 * renamed to textUtilities and added to blue by steven yi
 */

import java.util.ArrayList;

public class Main {
    public static String[] splitStringWithQuotes(String in) {
        char[] chars = in.trim().toCharArray();
        int state = 0;

        ArrayList<String> wordList = new ArrayList<String>();
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < chars.length; i++) {
            switch (state) {
            case 0:
                if (chars[i] == '\t' || chars[i] == ' ') {
                    continue;
                } else if (chars[i] == '\"') {
                    state = 2;
                } else if (chars[i] == '{') {
                    state = 3;
                } else {
                    buffer.append(chars[i]);
                    state = 1;
                }
                break;
            case 1:
                if (chars[i] == ' ' || chars[i] == '\t') {
                    wordList.add(buffer.toString());
                    buffer = new StringBuffer();
                    state = 0;
                } else {
                    buffer.append(chars[i]);
                }
                break;
            case 2:
                if (chars[i] == '\"') {
                    wordList.add(buffer.toString());
                    buffer = new StringBuffer();
                    state = 0;
                } else {
                    buffer.append(chars[i]);
                }
                break;
            case 3:
                if (chars[i] == '}') {
                    wordList.add(buffer.toString());
                    buffer = new StringBuffer();
                    state = 0;
                } else {
                    buffer.append(chars[i]);
                }
                break;
            }

        }
        wordList.add(buffer.toString());

        String[] retVal = new String[wordList.size()];
        wordList.toArray(retVal);

        return retVal;
    }
}

Related

  1. splitQuotedString(String str)
  2. splitQuotedString(String str)
  3. splitQuotedTokens(String str)
  4. splitSafeQuote(String input, char separator)
  5. splitShifted(int[] shiftedQuoteString)
  6. splitWithQuotes(String s)