is Contains Special Characters - Java java.lang

Java examples for java.lang:char

Description

is Contains Special Characters

Demo Code


import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.DataInputStream;
import java.util.List;
import java.util.Collections;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{
    public static boolean isContainsSpecialCharacters(String s) {
        String query = s;/* ww  w.  jav  a 2 s . co  m*/
        boolean bResult = false;
        String text = HelperFunctions.escapeQueryChars(query);
        if (!(text.equalsIgnoreCase(s))) {
            bResult = true;
        }
        return bResult;
    }
    /**
     * See: {@link org.apache.lucene.queryparser.classic queryparser syntax} 
     * for more information on Escaping Special Characters
     */
    public static String escapeQueryChars(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            // These characters are part of the query syntax and must be escaped
            if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '('
                    || c == ')' || c == ':' || c == '^' || c == '['
                    || c == ']' || c == '\"' || c == '{' || c == '}'
                    || c == '~' || c == '*' || c == '?' || c == '|'
                    || c == '&' || c == ';' || c == '/'
                    || Character.isWhitespace(c)) {
                sb.append('\\');
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

Related Tutorials