1

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

Boolean isGB2312(String chinese) {
    ......
}
0

2 Answers 2

3
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);
}
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.