Java Utililty Methods Wildcard

List of utility methods to do Wildcard

Description

The list of methods to do Wildcard are organized into topic(s).

Method

StringwildCard(final String _name)
This method appends wild cards before and after a string./
String trimmedName = _name.toString().trim();
if (trimmedName == null || trimmedName.equals("") || trimmedName.length() == 0) {
    return "";
if (trimmedName.length() > 0 && !trimmedName.endsWith("%")) {
    trimmedName = trimmedName + "%";
if (trimmedName.length() > 0 && !trimmedName.startsWith("%")) {
...
StringwildCard(String _name)
wild Card
String trimmedName = _name.toString().trim();
if (trimmedName == null || trimmedName.equals("") || trimmedName.length() == 0) {
    return "";
} else if (trimmedName.length() > 0 && !trimmedName.endsWith("%")) {
    return trimmedName + "%";
} else {
    return trimmedName;
StringwildCardBoth(String s)
_more_
return "%" + s + "%";
intwildcardCompare(String s, String z)
wildcard Compare
if (s.equals("*") || z.equals("*")) {
    return 0;
int l = s.length();
int m = z.length();
int i = 0;
int j = 0;
while (j < m && i < l) {
...
StringwildcardCriterionSQL(final String query)
Replaces the query with the wildcards asterisk "*" and interrogation mark "?"
String newQuery = query.replace('*', '%').replace('?', '_');
return newQuery;
StringwildcardIfEmpty(String inString)
wildcard If Empty
if ((inString == null) || (inString.equalsIgnoreCase(""))) {
    return "%";
return inString;
StringwildcardOrIsNullIfEmpty(String column, String inString)
wildcard Or Is Null If Empty
StringBuilder sb = new StringBuilder();
if ((inString == null) || (inString.equalsIgnoreCase(""))) {
    sb.append("'%' or ").append(column).append(" is null");
    return sb.toString();
sb.append("'").append(inString).append("'");
return sb.toString();
StringwildcardSearchToRegex(final String wildcard)
wildcard Search To Regex
StringBuilder buffer = new StringBuilder(wildcard.length() + 2);
buffer.append('^');
for (char c : wildcard.toCharArray()) {
    if (c == '*') {
        buffer.append(".*");
    } else if (c == '?') {
        buffer.append('.');
    } else if ("+()^$.{}[]|\\".indexOf(c) != -1) {
...
StringwildcardSqlToUser(String exp)
Convert an SQL LIKE/NOT LIKE expression to a * wildcard expression.
StringBuffer sb = new StringBuffer();
for (int i = 0; i < exp.length(); i++) {
    String substring = exp.substring(i);
    if (substring.startsWith("%")) {
        sb.append("*");
    } else {
        if (substring.startsWith("_")) {
            sb.append("?");
...
StringwildCardsToX(String s)
Replaces all '*' and '?'
if (s == null || s.isEmpty())
    return s;
StringBuilder res = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char ch = s.charAt(i);
    if (Character.isLetterOrDigit(ch) || ch == '-' || ch == '.') { 
    } else if (ch == '*' || ch == '?') { 
        ch = 'x';
...