Java String Tokenize getSQLTokens(String query)

Here you can find the source of getSQLTokens(String query)

Description

Returns a list of Strings representing the terms in the search string.

License

Open Source License

Parameter

Parameter Description
query The search query string.

Return

an ArrayList of Strings, containing the original query separated to individual search terms

Declaration

public static ArrayList<String> getSQLTokens(String query) 

Method Source Code

//package com.java2s;
/*//from   w ww.  j a va2 s.  c om
  MetaDB: A Distributed Metadata Collection Tool
  Copyright 2011, Lafayette College, Eric Luhrs, Haruki Yamaguchi, Long Ho.
    
  This file is part of MetaDB.
    
MetaDB 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 3 of the License, or
(at your option) any later version.
    
MetaDB 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 MetaDB.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

public class Main {
    /**
     * Returns a list of Strings representing the terms in the search string.
     * The words must be separated by whitespace and quotes signify an exact match.
     * @param query The search query string.
     * @return an ArrayList of Strings, containing the original query separated to individual search terms 
     */
    public static ArrayList<String> getSQLTokens(String query) {
        //Parse query into quoted/non-quoted words
        String[] tokens = query.split("#(?:\"([^\"]+)\")|([^ ]+)#");
        String noteTokens = "";
        for (String token : tokens)
            noteTokens += "\"" + token + "\" ";
        //MetaDbHelper.note("Search tokens: "+noteTokens);
        ArrayList<String> sqlTokens = new ArrayList<String>();
        int numTokens = tokens.length;
        for (int i = 0; i < numTokens; i++) {
            String quotesRemovedString = tokens[i].replaceAll("\"", "");
            sqlTokens.add("%" + quotesRemovedString.trim().toLowerCase() + "%");
        }
        return sqlTokens;
    }
}

Related

  1. getCommandTokens(String commandString)
  2. getFileName(StringTokenizer s)
  3. getNameTokens(final String names)
  4. getParamFloat(StringTokenizer s)
  5. getParamString(StringTokenizer s)
  6. getStringTokens(String str)
  7. getTokenArray(String sequence, String[] chain, boolean paired, boolean includeDelim)
  8. getTokens(final String s)
  9. getTokens(String msg, String delim)