代码之家  ›  专栏  ›  技术社区  ›  Peter Hilton

如何确定字符是否是Java中的字母?

  •  27
  • Peter Hilton  · 技术社区  · 16 年前

    如何检查一个字符串是否为字母-包括任何带重音符号的字母?

    我最近必须解决这个问题,所以在最近的vb6问题提醒我之后,我会自己回答。

    2 回复  |  直到 16 年前
        1
  •  33
  •   Michael Myers KitsuneYMG    16 年前


    matches() Pattern Character.isLetter()

    import java.util.regex.*;
    
    class TestLetter {
        private static final Pattern ONE_CHAR_PATTERN = Pattern.compile("\\p{L}");
        private static final int NUM_TESTS = 10000000;
    
        public static void main(String[] args) {
            long start = System.nanoTime();
            int counter = 0;
            for (int i = 0; i < NUM_TESTS; i++) {
                if (testMatches(Character.toString((char) (i % 128))))
                    counter++;
            }
            System.out.println(NUM_TESTS + " tests of Pattern.matches() took " +
                    (System.nanoTime()-start) + " ns.");
            System.out.println("There were " + counter + "/" + NUM_TESTS +
                    " valid characters");
            /*********************************/
            start = System.nanoTime();
            counter = 0;
            for (int i = 0; i < NUM_TESTS; i++) {
                if (testCharacter(Character.toString((char) (i % 128))))
                    counter++;
            }
            System.out.println(NUM_TESTS + " tests of isLetter() took " +
                    (System.nanoTime()-start) + " ns.");
            System.out.println("There were " + counter + "/" + NUM_TESTS +
                    " valid characters");
            /*********************************/
            start = System.nanoTime();
            counter = 0;
            for (int i = 0; i < NUM_TESTS; i++) {
                if (testMatchesNoCache(Character.toString((char) (i % 128))))
                    counter++;
            }
            System.out.println(NUM_TESTS + " tests of String.matches() took " +
                    (System.nanoTime()-start) + " ns.");
            System.out.println("There were " + counter + "/" + NUM_TESTS +
                    " valid characters");
        }
    
        private static boolean testMatches(final String c) {
            return ONE_CHAR_PATTERN.matcher(c).matches();
        }
        private static boolean testMatchesNoCache(final String c) {
            return c.matches("\\p{L}");
        }
        private static boolean testCharacter(final String c) {
            return Character.isLetter(c.charAt(0));
        }
    }
    

    10000000 tests of Pattern.matches() took 4325146672 ns.
    There were 4062500/10000000 valid characters
    10000000 tests of isLetter() took 546031201 ns.
    There were 4062500/10000000 valid characters
    10000000 tests of String.matches() took 11900205444 ns.
    There were 4062500/10000000 valid characters

        2
  •  22
  •   Peter Hilton    16 年前

    string.matches("\\p{L}"); // Unicode letter
    string.matches("\\p{Lu}"); // Unicode upper-case letter
    

    Character.isLetter(character);