Java String Tokenize tokenize(String str, String delims)

Here you can find the source of tokenize(String str, String delims)

Description

Split a string in a list of tokens delimited by a specified character.

License

Open Source License

Parameter

Parameter Description
delims the list of characters to be used as delimiters

Return

the list of tokens

Declaration

static public Vector<String> tokenize(String str, String delims) 

Method Source Code


//package com.java2s;
/*//from ww  w .  j  a  va2s.  c o m
*   EuroCarbDB, a framework for carbohydrate bioinformatics
*
*   Copyright (c) 2006-2009, Eurocarb project, or third-party contributors as
*   indicated by the @author tags or express copyright attribution
*   statements applied by the authors.  
*
*   This copyrighted material is made available to anyone wishing to use, modify,
*   copy, or redistribute it subject to the terms and conditions of the GNU
*   Lesser General Public License, as published by the Free Software Foundation.
*   A copy of this license accompanies this distribution in the file LICENSE.txt.
*
*   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 Lesser General Public License
*   for more details.
*
*   Last commit: $Rev: 1210 $ by $Author: glycoslave $ on $Date:: 2009-06-12 #$  
*/

import java.util.*;

public class Main {
    /**
       Split a string in a list of tokens delimited by a specified
       character.
       @param delims the list of characters to be used as delimiters
       @return the list of tokens
     */
    static public Vector<String> tokenize(String str, String delims) {
        Vector<String> out = new Vector<String>();
        if (str == null || str.length() == 0 || delims == null || delims.length() == 0)
            return out;

        StringBuilder token = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            if (delims.indexOf(str.charAt(i)) != -1) {
                if (token.length() > 0) {
                    out.addElement(token.toString());
                    token = new StringBuilder(str.length());
                }
            } else {
                token.append(str.charAt(i));
            }
        }
        if (token.length() > 0)
            out.addElement(token.toString());

        return out;
    }

    /**
       Split a string in a list of tokens delimited by a specified
       character. Keep together the parts of the string inside
       parenthesis.
       @param delims the list of characters to be used as delimiters
       @param open_par the character to be used as open parenthesis
       @param closed_par the character to be used as closed parenthesis
       @return the list of tokens
     */
    static public Vector<String> tokenize(String str, String delims, char open_par, char closed_par) {
        Vector<String> out = new Vector<String>();
        if (str == null || str.length() == 0 || delims == null || delims.length() == 0)
            return out;

        StringBuilder token = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == open_par) {
                token.append(str.charAt(i));
                for (++i; i < str.length() && str.charAt(i) != closed_par; i++)
                    token.append(str.charAt(i));
                if (i < str.length())
                    token.append(str.charAt(i));
            } else {
                if (delims.indexOf(str.charAt(i)) != -1) {
                    if (token.length() > 0) {
                        out.addElement(token.toString());
                        token = new StringBuilder(str.length());
                    }
                } else {
                    token.append(str.charAt(i));
                }
            }
        }
        if (token.length() > 0)
            out.addElement(token.toString());

        return out;
    }

    /**
       Return a string representation of an integer array. 
       @param delim the character to be used as delimiter between the
       array's elements
     */
    static public String toString(int[] v, char delim) {
        if (v == null)
            return "";

        StringBuilder strbuf = new StringBuilder();
        for (int i = 0; i < v.length; i++) {
            if (i > 0)
                strbuf.append(delim);
            strbuf.append(v[i]);
        }
        return strbuf.toString();
    }

    /**
       Return a string representation of an object array. 
       @param delim the character to be used as delimiter between the
       array's elements
     */
    static public String toString(Object[] v, char delim) {
        if (v == null)
            return "";

        StringBuilder strbuf = new StringBuilder();
        for (int i = 0; i < v.length; i++) {
            if (i > 0)
                strbuf.append(delim);
            strbuf.append(v[i].toString());
        }
        return strbuf.toString();
    }

    /**
       Return a string representation of a list. 
       @param delim the character to be used as delimiter between the
       list's elements
     */
    static public String toString(Collection<? extends Object> v, char delim) {
        if (v == null)
            return "";

        StringBuilder strbuf = new StringBuilder();
        for (Object o : v) {
            if (strbuf.length() > 0)
                strbuf.append(delim);
            strbuf.append(o.toString());
        }
        return strbuf.toString();
    }
}

Related

  1. Tokenize(String sent)
  2. tokenize(String sentence)
  3. tokenize(String source, char separator)
  4. tokenize(String source, char separator)
  5. tokenize(String str, char delim)
  6. tokenize(String str, String delims, boolean returnDelims)
  7. tokenize(String string, String splitOn)
  8. tokenize(String text)
  9. tokenize(String text)