Java Java String Format toJavaPattern(String pattern)

Here you can find the source of toJavaPattern(String pattern)

Description

to Java Pattern

License

Open Source License

Declaration

private static String toJavaPattern(String pattern) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static String toJavaPattern(String pattern) {
        StringBuilder result = new StringBuilder("^");
        char metachar[] = { '$', '^', '[', ']', '(', ')', '{', '|', /* '*', */'+', '?', '.', '/' };
        for (int i = 0; i < pattern.length(); i++) {
            char ch = pattern.charAt(i);
            boolean isMeta = false;
            for (char aMetachar : metachar) {
                if (ch == aMetachar) {
                    result.append("\\").append(String.valueOf(ch));
                    isMeta = true;// ww w  . j a v a2  s  .  com
                    break;
                }
            }
            if (!isMeta) {
                if (ch == '*') {
                    result.append(".*");
                } else {
                    result.append(ch);
                }

            }
        }
        result.append("$");
        return result.toString();
    }
}

Related

  1. toJavaName(String name)
  2. toJavaName(String name)
  3. toJavaPackageName(String name)
  4. toJavaPath(String path)
  5. toJavaPath(String path)
  6. toJavascriptArray(String[][] Vals)
  7. toJavaScriptEncode(String string)
  8. toJavaScriptSafeString(String content)
  9. toJavaSecs(long secs)