Java Wildcard wildCardsToX(String s)

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

Description

Replaces all '*' and '?'

License

Open Source License

Parameter

Parameter Description
s source string

Return

the resulting string

Declaration

static public String wildCardsToX(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
* Copyright (c) 2015 ARM Ltd. and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:/*from w w  w . j  ava 2  s  . c  om*/
* ARM Ltd and ARM Germany GmbH - Initial API and implementation
*******************************************************************************/

public class Main {
    /**
     * Replaces all '*' and '?' to 'x'  and all non-alphanumeric chars to '_' in supplied string  
     * @param s source string
     * @return the resulting string
     */
    static public String wildCardsToX(String s) {
        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 == '.') { // allowed characters
                // do nothing
            } else if (ch == '*' || ch == '?') { // wildcards
                ch = 'x';
            } else {// any other character
                ch = '_';
            }
            res.append(ch);
        }
        return res.toString();
    }
}

Related

  1. wildcardCriterionSQL(final String query)
  2. wildcardIfEmpty(String inString)
  3. wildcardOrIsNullIfEmpty(String column, String inString)
  4. wildcardSearchToRegex(final String wildcard)
  5. wildcardSqlToUser(String exp)
  6. wildcardToCaseInsensitiveRegex(final String pattern)
  7. wildcardUserToSql(String exp)