读书人

技术有关问题:java中怎么判断字符串内

发布时间: 2012-05-16 23:40:10 作者: rapoo

技术问题:java中如何判断字符串内容是否是编码

一个比较偏僻的问题:

如果字符串的内容是一种编码格式:

String str = "GBK";



String str = "utf-8";

则返回true,如果字符串的内容不是编码格式(如String str = "srtergerg"; )则返回false



[解决办法]

Java code
import java.nio.charset.Charset;public class CharsetTest {    public static void main(String[] args) {        System.out.println(Charset.isSupported("GBK"));        System.out.println(Charset.isSupported("utf-8"));        System.out.println(Charset.isSupported("iso-8859-1"));        System.out.println(Charset.isSupported("hello"));        System.out.println(Charset.isSupported("gbK"));        System.out.println(Charset.isSupported("gbk"));        System.out.println(Charset.isSupported("GB18030"));    }}
[解决办法]
boolean flag = CharSet.isSupported("GBK")

读书人网 >J2SE开发

热点推荐