Java String Tokenize tokenize(String s)

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

Description

tokenize

License

Apache License

Parameter

Parameter Description
s a parameter

Return

element toklenized in array elements

Declaration

public static String[] tokenize(String s) 

Method Source Code

//package com.java2s;
/**/*  w w  w. ja  va2s .c  om*/
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/

import java.util.*;

public class Main {
    /**
     *
     * @param s
     * @return element toklenized in array elements
     */
    public static String[] tokenize(String s) {
        return tokenize(s, null, (char) 0, 0);
    }

    /**
     *
     * @param s
     * @param tok
     * @return element toklenized in array elements
     */
    public static String[] tokenize(String s, String tok) {
        return tokenize(s, tok, (char) 0, 0);
    }

    /**
     *
     * @param s
     * @param tok
     * @param quotes
     * @return element toklenized in array elements
     */
    public static String[] tokenize(String s, String tok, char quotes) {
        return tokenize(s, tok, quotes, 0);
    }

    /**
     *
     * @param s
     * @param tok
     * @param len
     * @return element toklenized in array elements
     */
    public static String[] tokenize(String s, String tok, int len) {
        return tokenize(s, tok, (char) 0, len);
    }

    /**
     *
     * @param s
     * @param tok
     * @param quotes
     * @param len
     * @return element toklenized in array elements
     */
    public static String[] tokenize(String s, String tok, char quotes, int len) {
        if (s == null || "".equals(s.trim())) {
            if (len > 0) {
                String t[] = new String[len];
                for (int i = 0; i < len; i++) {
                    t[i] = "";
                }
                return t;
            } else if (len < 0) {
                String t[] = new String[0];
                return t;
            }
            return null;
        }
        StringTokenizer st = null;
        if (quotes != 0) {
            String t[] = tokenize(s, "" + quotes + "", (char) 0);
            int j = (s.trim().charAt(0) == quotes ? 0 : 1);
            Vector v = new Vector();
            for (int i = 0; i < t.length; i++) {
                if (i % 2 == j) {
                    v.addElement(t[i]);
                } else {
                    String w[] = tokenize(t[i], tok);
                    if (w != null) {
                        for (int k = 0; k < w.length; k++) {
                            v.addElement(w[k]);
                        }
                    } //if
                } //if-else
            } //for
            t = new String[v.size()];
            v.copyInto(t);
            return t;
        }
        if (tok == null) {
            st = new StringTokenizer(s.trim());
        } else {
            st = new StringTokenizer(s.trim(), tok);
        }
        if (len == 0) {
            len = st.countTokens();
        }
        int length = Math.abs(len);
        String t[] = new String[length];
        int i = 0;
        for (; st.hasMoreTokens() && i < length; i++) {
            t[i] = st.nextToken().trim();
        }
        if (len > 0) {
            for (; i < length; i++) {
                t[i] = "";
            }
        } else {
            char c = ' ';
            if (tok != null) {
                c = tok.charAt(0);
            }
            for (i--; st.hasMoreTokens();) {
                t[i] += ("" + c + st.nextToken().trim());
            }
        }
        return t;
    }

    /**
     * Esegue una Trim su una Stringa
     * se s == null return null
     *
     *   @param s String to trim
     * @return La Stringa Trimmata
     */
    public static final String trim(String s) {
        if (s == null) {
            return null;
        }
        return s.trim();
    }
}

Related

  1. tokenize(String input)
  2. tokenize(String input)
  3. tokenize(String input, char by)
  4. tokenize(String input, String delim)
  5. tokenize(String line)
  6. tokenize(String s)
  7. tokenize(String s)
  8. tokenize(String s)
  9. tokenize(String s)