Here you can find the source of split(String string)
Parameter | Description |
---|---|
string | the String that should be split |
public static String[] split(String string)
//package com.java2s; /*==========================================================================*\ | $Id: ShellStringUtils.java,v 1.3 2009/09/13 12:59:29 aallowat Exp $ |*-------------------------------------------------------------------------*| | Copyright (C) 2006-2009 Virginia Tech | | This file is part of Web-CAT Eclipse Plugins. | | Web-CAT 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. | | Web-CAT 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 Web-CAT; if not, see <http://www.gnu.org/licenses/>. \*==========================================================================*/ import java.util.ArrayList; public class Main { /**//from w w w. j a v a2 s .c o m * State identifiers used by the DFA in the split method. */ private static final int STATE_BEGIN = 0; private static final int STATE_PART = 1; private static final int STATE_ESCAPE_IN_PART = 2; private static final int STATE_DQUOTE = 3; private static final int STATE_ESCAPE_IN_DQUOTE = 4; private static final int STATE_SQUOTE = 5; private static final int STATE_ESCAPE_IN_SQUOTE = 6; /** * Splits a string into an array of components. Whitespace is used as the * delimiter, but single and double quotes are handled properly so that a * component in quotes is extracted as a single component, regardless of * whether it contains whitespace or not. * * @param string * the String that should be split * * @return an array of String components extracted from the string */ public static String[] split(String string) { ArrayList<String> parts = new ArrayList<String>(); StringBuffer currentPart = new StringBuffer(); int state = STATE_BEGIN; for (int i = 0; i < string.length(); i++) { char ch = string.charAt(i); switch (state) { case STATE_BEGIN: if (ch == '"') { currentPart.append(ch); state = STATE_DQUOTE; } else if (ch == '\'') { currentPart.append(ch); state = STATE_SQUOTE; } else if (ch == '\\') { state = STATE_ESCAPE_IN_PART; } else if (!Character.isWhitespace(ch)) { currentPart.append(ch); state = STATE_PART; } break; case STATE_PART: if (Character.isWhitespace(ch)) { parts.add(currentPart.toString()); currentPart = new StringBuffer(); state = STATE_BEGIN; } else if (ch == '"') { currentPart.append(ch); state = STATE_DQUOTE; } else if (ch == '\'') { currentPart.append(ch); state = STATE_SQUOTE; } else if (ch == '\\') { currentPart.append(ch); state = STATE_ESCAPE_IN_PART; } else { currentPart.append(ch); } break; case STATE_ESCAPE_IN_PART: currentPart.append(ch); state = STATE_PART; break; case STATE_DQUOTE: currentPart.append(ch); if (ch == '"') { state = STATE_PART; } else if (ch == '\\') { state = STATE_ESCAPE_IN_DQUOTE; } break; case STATE_ESCAPE_IN_DQUOTE: currentPart.append(ch); state = STATE_DQUOTE; break; case STATE_SQUOTE: currentPart.append(ch); if (ch == '\'') { state = STATE_PART; } else if (ch == '\\') { state = STATE_ESCAPE_IN_SQUOTE; } break; case STATE_ESCAPE_IN_SQUOTE: currentPart.append(ch); state = STATE_SQUOTE; break; } } if (currentPart.length() > 0) parts.add(currentPart.toString()); String[] array = new String[parts.size()]; parts.toArray(array); return array; } }