javagb2312

How to find whether a character is GB2312 in java


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

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

Solution

  • 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);
    }