Java Wildcard Match wildCardMatch(String text, String pattern)

Here you can find the source of wildCardMatch(String text, String pattern)

Description

test the text match the wild char "*"

License

Open Source License

Parameter

Parameter Description
text example: helloworld
pattern example: hell*world

Declaration

public static boolean wildCardMatch(String text, String pattern) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  www  . j  a  v  a 2  s  .  co m*/
     * test the text match the wild char "*"
     * @param text    example: helloworld
     * @param pattern example: hell*world
     * @return 
     * @author <a href="mailto:iffiff1@gmail.com">Tyler Chen</a> 
     * @since 2015-4-8
     */
    public static boolean wildCardMatch(String text, String pattern) {
        if (pattern == null || pattern.length() < 1 || "*".equals(pattern)) {
            return true;
        }
        if (text == null || text.length() < 1) {
            return false;
        }
        String[] cards = pattern.split("\\*");
        for (String card : cards) {
            int idx = text.indexOf(card);
            if (idx == -1) {
                return false;
            }
            text = text.substring(idx + card.length());
        }
        return true;
    }
}

Related

  1. wildcardMatch(String pattern, String string)
  2. wildcardMatch(String string, String pattern)
  3. wildCardMatch(String text, String pattern)
  4. wildCardMatch(String text, String pattern)
  5. wildCardMatch(String text, String pattern)
  6. wildCardMatch(String text, String wildcard)
  7. wildcardMatches(String pattern, String text)