Java String to Array stringToArray(String s)

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

Description

string To Array

License

Open Source License

Declaration

public static String[] stringToArray(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 Synopsys, Incorporated
 * 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  . java 2  s . com*/
 * Synopsys, Inc - Initial implementation 
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] stringToArray(String s) {
        List<String> list = stringToList(s);
        return list.toArray(new String[list.size()]);
    }

    /**
    * Split a string into a list using whitespace as
    * the separator. The list to be filled in is passed as the first
    * argument.
    * <p>
    * Embedded quotes that aren't excaped are assume to
    * surround a single token. Thus:
    * <p>
    * <pre><code>
    *  This "is a" string wi'th embed'ded substrings
    * </pre></code>
    * <p>
    * Will parse as:
    * <pre><code>
    *  "This"
    *  "is a"
    *  "string"
    *  "with embedded"
    *  "substrings"
    * </pre></code>
    * 
    * <p>
    * @param l the list to be filled in.
    * @param s the string to be split into a list.
    * @return the list <code>l</code>
    */
    public static List<String> stringToList(List<String> l, String s) {
        if (s != null) {
            int len = s.length();
            int i = 0;
            char c;
            StringBuffer buf = new StringBuffer(s.length());

            while (i < len) {
                buf.delete(0, buf.length());

                // Skip white space
                for (; i < len; i++) {
                    c = s.charAt(i);
                    if (!Character.isWhitespace(c))
                        break;
                }

                // Scan a token, taking into account
                // quoted substrings. We strip off the quotes
                // in a manner similar to Cshell.
                char quote = 0;
                for (; i < len; i++) {
                    c = s.charAt(i);
                    if (c == '\\' && quote != 0) {
                        if (i + 1 < len) {
                            i++;
                            buf.append(s.charAt(i));
                        }
                    } else if (quote == 0) {
                        if (Character.isWhitespace(c))
                            break;
                        if (c == '"' || c == '\'') {
                            quote = c;
                        } else
                            buf.append(c);
                    } else if (c == quote)
                        quote = 0;
                    else
                        buf.append(c);
                }
                l.add(buf.toString());
            }
        }

        return l;
    }

    /**
     * Split a string into a list using whitespace as
     * the separator.
     * @param s the string to be split.
     * @return the list.
     */
    public static List<String> stringToList(String s) {
        return stringToList(new ArrayList<String>(s.length() / 5 + 3), s);
    }
}

Related

  1. stringToArray(final String data, final String delim)
  2. stringToArray(String charc, String commas)
  3. stringToArray(String inputVal, String delimiter)
  4. stringToArray(String str, String separator)
  5. stringToByteArray(String text, int length)