Java String Tokenize tokenize(String text, char separator)

Here you can find the source of tokenize(String text, char separator)

Description

Splits a list's String representation into tokens.

License

Open Source License

Parameter

Parameter Description
text the String representation of a list.
separator the character used to separate tokens

Return

an array of tokens.

Declaration

public static String[] tokenize(String text, char separator) 

Method Source Code


//package com.java2s;
/*/*from www. j  a va 2  s.  c  om*/
 * (c) Copyright 2007-2012 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.*;

public class Main {
    /**
     * Splits a list's String representation into tokens.
     * @param text the String representation of a list.
     * @param separator the character used to separate tokens
     * @return an array of tokens.
     */
    public static String[] tokenize(String text, char separator) {
        if (text == null)
            return null;
        if (text.length() == 0)
            return new String[] { "" };
        int i = 0;
        int sep;
        List<String> list = new ArrayList<String>();
        while ((sep = text.indexOf(separator, i)) >= 0) {
            if (sep == i) {
                list.add("");
                i++;
            } else {
                list.add(text.substring(i, sep));
                i = sep + 1;
            }
        }
        if (i < text.length())
            list.add(text.substring(i));
        else if (text.endsWith("" + separator))
            list.add("");
        String[] tokens = new String[list.size()];
        for (i = 0; i < tokens.length; i++)
            tokens[i] = list.get(i);
        return tokens;
    }
}

Related

  1. tokenize(String str, String delims, boolean returnDelims)
  2. tokenize(String string, String splitOn)
  3. tokenize(String text)
  4. tokenize(String text)
  5. tokenize(String text)
  6. tokenize(String text, char separator)
  7. tokenize(String text, String delimiter)
  8. tokenize(String value)
  9. tokenize(String value)