如何获得所有字母的集合在Java / Clojure? [英] How do I get the set of all letters in Java/Clojure?

查看:208
本文介绍了如何获得所有字母的集合在Java / Clojure?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我可以这样做:

In Python, I can do this:

>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

有什么办法做类似的Clojure除了复制和粘贴上述字符的地方)?我查看了Clojure标准库和java标准库,但找不到它。

Is there any way to do something similar in Clojure (apart from copying and pasting the above characters somewhere)? I looked through both the Clojure standard library and the java standard library and couldn't find it.

推荐答案

中心实现:

private static String allLetters(String charsetName)
{
    CharsetEncoder ce = Charset.forName(charsetName).newEncoder();
    StringBuilder result = new StringBuilder();
    for(char c=0; c<Character.MAX_VALUE; c++)
    {
        if(ce.canEncode(c) && Character.isLetter(c))
        {
            result.append(c);
        }
    }
    return result.toString();
}

使用US-ASCII调用此函数, (除了大写字母先出现)。你可以用 Charset.defaultCharset()来调用它,但是我怀疑你在大多数系统上得到的远远大于ASCII字符,即使在美国。

Call this with "US-ASCII" and you'll get the desired result (except that uppercase letters come first). You could call it with Charset.defaultCharset(), but I suspect that you'd get far more than the ASCII letters on most systems, even in the USA.

注意:只考虑基本的多语言飞机。不会太难延伸到辅助飞机,但它需要很长时间,效用是有问题的。

Caveat: only considers the basic multilingual plane. Wouldn't be too hard to extend to the supplementary planes, but it would take a lot longer, and the utility is questionable.

这篇关于如何获得所有字母的集合在Java / Clojure?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆