Java String Tokenize tokenize(String s)

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

Description

Tokenize string s into an array

License

Open Source License

Declaration

public static String[] tokenize(String s) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /** Tokenize string s into an array */
    public static String[] tokenize(String s) {
        ArrayList<String> r = new ArrayList<String>();
        String remain = s;//ww  w  .j  a  v a2  s . com
        int n = 0, pos;

        remain = remain.trim();
        while (remain.length() > 0) {
            if (remain.startsWith("\"")) {
                // Field in quotes
                pos = remain.indexOf('"', 1);
                if (pos == -1)
                    break;
                r.add(remain.substring(1, pos));
                if (pos + 1 < remain.length())
                    pos++;
            } else {
                // Space-separated field
                pos = remain.indexOf(' ', 0);
                if (pos == -1) {
                    r.add(remain);
                    remain = "";
                } else
                    r.add(remain.substring(0, pos));
            }
            remain = remain.substring(pos + 1);
            remain = remain.trim();
            // - is used as a placeholder for empy fields
            if (r.get(n).equals("-"))
                r.set(n, "");
            n++;
        }
        return r.toArray(new String[0]);
    }
}

Related

  1. tokenize(String line)
  2. tokenize(String s)
  3. tokenize(String s)
  4. tokenize(String s)
  5. tokenize(String s)
  6. tokenize(String s, char separator)
  7. tokenize(String s, char separator, int maxTokens)
  8. tokenize(String s, char separator, int maxTokens)
  9. tokenize(String s, String delimiters)