2

Thanks in advance for your patience. This is my problem. I'm writing a program in Java that works best with a big set of different characters. I have to store all the characters in a String. I started with private static final String values = "0123456789"; Then I added A-Z, a-z and all the commons symbols. But they are still too few, so I tought that maybe Unicode could be the solution.

The problem is now: what is the best way to get all the unicode characters that can be displayed in Eclipse (my algorithm will probably fail if there are unrecognized characters - those displayed like little rectangles). Is it possible to build a string (or some strings) with all the characters present here (en.wikipedia.org/wiki/List_of_Unicode_characters) correctly displayed?

I can do a rough copy-paste from http://www.terena.org/activities/multiling/euroml/tests/test-ucspages1ucs.html or http://zenoplex.jp/tools/unicoderange_generator.html, but I would appreciate some cleaner solution. I don't know if there is a way to extract characters fron a font (the Unifont one). Or maybe I should parse this (www. utf8-chartable.de/unicode-utf8-table.pl) webpage.

Moreover, by adding all the characters into a String I will probably get the error: "The type generates a string that requires more than 65535 bytes to encode in Utf8 format in the constant pool" (discussed in this question on SO: /questions/10798769/how-to-process-a-string-with-823237-characters).

Hybrid solutions can be accepted. I can remove duplicates following this question on SO questions/4989091/removing-duplicates-from-a-string-in-java)

Finally: every solution to get the longest only-different-characters string is accepted. Thanks!

4

2 回答 2

3
于 2013-10-18T17:14:52.743 回答
0

只需使用 Apache 类,org.apache.commons.lang.RandomStringUtils(commons-lang)就可以解决您的目的。

http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/RandomStringUtils.html

api使用请参考下面的代码,

    import org.apache.commons.lang3.RandomStringUtils;

public class RandomString {

    public static void main(String[] args) {


        // Random string only with numbers

  String string = RandomStringUtils.random(64, false, true);

  System.out.println("Random 0 = " + string);


  // Random alphabetic string

  string = RandomStringUtils.randomAlphabetic(64);

  System.out.println("Random 1 = " + string);


  // Random ASCII string

  string = RandomStringUtils.randomAscii(32);

  System.out.println("Random 2 = " + string);


  // Create a random string with indexes from the given array of chars  

  string = RandomStringUtils.random(32, 0, 20, true, true, "bj81G5RDED3DC6142kasok".toCharArray());

  System.out.println("Random 3 = " + string);
    }
}
于 2013-10-18T16:12:54.690 回答