繁体   English   中英

验证特定代码页中是否完全支持字符串

[英]Verifying if a String is fully supported in a specific codepage

下面我有一个小型测试程序,可以验证代码页 CP852是否完全支持字符串的内容。 有没有更优雅的方式做到这一点?

public class CodepageTest {
    public static void main(String[] args) {
        try {
            String test = "æøå123";
            String test2 = new String(test.getBytes("CP852"), "CP852");

            System.out.println(test);
            System.out.println(test2);

            System.out.println("String supported in codepage: " + (test.equals(test2)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

输出:

æøå123
???123
String supported in codepage: false

您可以显式创建一个与您的字符集相对应的编码器,并使用canEncode方法

boolean canEncode = Charset.forName("CP852").newEncoder().canEncode(test);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM