Java String Tokenize tokenize(String s)

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

Description

tokenize

License

Open Source License

Declaration

public static List<String> tokenize(String s) 

Method Source Code

//package com.java2s;
/*//  w w  w.  ja va 2  s . co  m
 * Utilities.java
 *
 * Copyright (C) 1998-2005 Peter Graves
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> tokenize(String s) {
        ArrayList<String> list = new ArrayList<String>();
        if (s != null) {
            StringBuilder sb = new StringBuilder();
            boolean inQuote = false;
            final int limit = s.length();
            for (int i = 0; i < limit; i++) {
                char c = s.charAt(i);
                switch (c) {
                case ' ':
                    if (inQuote)
                        sb.append(c);
                    else if (sb.length() > 0) {
                        list.add(sb.toString());
                        sb.setLength(0);
                    }
                    break;
                case '"':
                    if (inQuote) {
                        if (sb.length() > 0) {
                            list.add(sb.toString());
                            sb.setLength(0);
                        }
                        inQuote = false;
                    } else
                        inQuote = true;
                    break;
                default:
                    sb.append(c);
                    break;
                }
            }
            if (sb.length() > 0)
                list.add(sb.toString());
        }
        return list;
    }
}

Related

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