一个比较偏僻的问题:
如果字符串的内容是一种编码格式:
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")