如何判断字符是否是Java中的字母?

时间:2008-09-18 16:07:49

标签: java unicode

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

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

2 个答案:

答案 0 :(得分:33)

Character.isLetter()比string.matches()快得多,因为string.matches()每次都会编译一个新的Pattern。即使缓存模式,我认为是Letter()仍会击败它。


编辑:刚刚再遇到这个问题,我想我会想出一些实际数字。这是我尝试基准测试,检查所有三种方法(matches()是否有PatternCharacter.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

即使使用缓存Pattern,这几乎要好8倍。 (并且未缓存比缓存差3倍。)

答案 1 :(得分:22)

只是检查一封信是否在A-Z中,因为它不包含带有重音符号或其他字母表中字母的字母。

我发现您可以将正则表达式类用于“Unicode字母”或其区分大小写的变体之一:

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

您也可以使用 Character 类:

执行此操作
Character.isLetter(character);

但如果您需要检查多个字母,则不太方便。