Java String Split by Word splitKeyWords(String sql)

Here you can find the source of splitKeyWords(String sql)

Description

split Key Words

License

Open Source License

Declaration

public static List<String> splitKeyWords(String sql) 

Method Source Code

//package com.java2s;
/*******************************************************************************************
 *   Copyright (c) 2016, zzg.zhou(11039850@qq.com)
 * // w  ww  .j  a v a  2s .c om
 *  Monalisa is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
    
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> splitKeyWords(String sql) {
        List<String> kws = new ArrayList<String>();

        StringBuffer word = new StringBuffer();
        for (int i = 0; i < sql.length(); i++) {
            char c = sql.charAt(i);
            if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
                if (word.length() > 0) {
                    kws.add(word.toString().toUpperCase());

                    word.delete(0, word.length());
                }
            } else if (c == '=' || c == '>' || c == '<' || c == '!' || c == ',') {
                if (word.length() > 0) {
                    kws.add(word.toString().toUpperCase());

                    word.delete(0, word.length());
                }
                kws.add(String.valueOf(c));
            } else {
                if (c == '\'') {
                    int from = i;

                    i++;
                    for (; i < sql.length(); i++) {
                        c = sql.charAt(i);
                        if (c == '\\') {
                            i++;
                        } else if (c == '\'') {
                            break;
                        }
                    }

                    kws.add(sql.substring(from, i + 1));
                } else if (c == '"') {
                    int from = i;

                    i++;
                    for (; i < sql.length(); i++) {
                        c = sql.charAt(i);
                        if (c == '\\') {
                            i++;
                        } else if (c == '"') {
                            break;
                        }
                    }

                    kws.add(sql.substring(from, i + 1));
                } else {
                    word.append(c);
                }
            }
        }

        if (word.length() > 0) {
            kws.add(word.toString().toUpperCase());

            word.delete(0, word.length());
        }

        return kws;
    }
}

Related

  1. split(String words, String character)
  2. splitIdentifierToWords(String str)
  3. splitIntoWords(String s)
  4. splitOne(String wordString)
  5. splitUnit(String word)
  6. splitWord(List words, int listIndex)
  7. splitWords(String name)