中获取所有字母的集合

中获取所有字母的集合

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

问题描述

在 Python 中,我可以这样做:

>>>导入字符串>>>字符串.字母'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

有没有什么办法可以在 Clojure 中做类似的事情(除了在某处复制和粘贴上述字符)?Clojure标准库和java标准库都看了一遍,没找到.

解决方案

一个适当的非 ASCII 中心实现:

private static String allLetters(String charsetName){CharsetEncoder ce = Charset.forName(charsetName).newEncoder();StringBuilder 结果 = new StringBuilder();for(char c=0; c

用US-ASCII"调用它,你会得到想要的结果(除了大写字母在前).您可以使用 Charset.defaultCharset() 来调用它,但我怀疑在大多数系统上,即使在美国,您得到的也远不止 ASCII 字母.

警告:只考虑基本的多语言平面.扩展到辅助位面不会太难,但会花费更长的时间,实用性有问题.

In Python, I can do this:

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

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.

解决方案

A properly non-ASCII-centric implementation:

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();
}

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 中获取所有字母的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:38