tags:

views:

39

answers:

2

I would like write a java function like: if one char is not in GB2312, return false

Boolean isGB2312(String chinese) {
    ......
}
A: 

I haven't used Java for a while, but I know of Iconv, which can throw an exception when there is illegal character, so you can return false when an exception is caught, and return true when the conversion to UTF-8 went through without problem.

動靜能量
+2  A: 
import java.nio.charset.*;

class Some{

public static void main(String args[]) 
 {
final Charset cs = Charset.forName("GB2312");
final CharsetEncoder encode = cs.newEncoder();
System.out.println(encode.canEncode("ダチヂッツヅテデ")); 
 }

}

UPDATE: As a static method:

final static boolean isGB2312(final String s)
{
return java.nio.charset.Charset.forName("GB2312").newEncoder().canEncode(s);
}
Michael Konietzka