Check character string coding tools
// knowledge
//str.equals(new String (str.getBytes (), encode )): check coding
// default encoding system: System.getProperty ( "file.encoding")
// default character encoding: Charset .defaultCharset ()
// default system language code: System.getProperty ( "user.language")
package baseutil; import java.nio.charset.Charset; / ** * @author Ma Jiali * @Version Created: 2019 5:47:49 PM December 7 * @Description: TODO check character string coding tools */ public class EncodingUtil { /** * @Title:getEncoding * @Author : Ma Jiali * @Date: 2019 Nian 6:02:27 PM December 7 * @Description: TODO check character string coding * @Param str-- coded character string to be identified * @return String */ public static String getEncoding(String str) { Encode String = "encoding format unrecognized" ; the try { encode = "UTF-16"; if (str.equals(new String(str.getBytes(), encode))) { return encode; } encode = "UTF-8"; if (str.equals(new String(str.getBytes(), encode))) { return encode; } encode = "the ASCII" ; IF (str.equals ( new new String (str.getBytes (), encode))) { return "character string <<" + str + ">> only of numbers and English letters can not be recognized which encoding format " ; } encode = "ISO-8859-1"; if (str.equals(new String(str.getBytes(), encode))) { return encode; } encode = "GB2312"; if (str.equals(new String(str.getBytes(), encode))) { return encode; } } catch (Exception e) { e.printStackTrace (); } return encode; } /** * @Title:isEncoding * @Author : Ma Jiali * @Date: 2019 Nian 6:12:17 PM December 7 * @Description: TODO check whether the string is passed parameter coding * @Param string to be verified str-- * @Param encode-- character encoding: UTF-16, UTF-8 , ASCII, ISO-8859-1, GB2312 , etc. * @return boolean */ public static boolean isEncoding(String str, String encode) { try { if (str.equals(new String(str.getBytes(), encode))) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace (); } return false; } public static void main(String[] args) { /** * - test string encoding * / System.out.println ( "encoding system default:" + the System.getProperty ( "the file.encoding" )); System.out.println ( "system default character encoding:" + Charset.defaultCharset ()); System.out.println ( "system default language code:" + the System.getProperty ( "user.language" )); The Hello String = "the Hello, I'm coming!" ; System.out.println ( " '! The Hello, I'm coming' code is:" + getEncoding (the Hello)); / ** * - check character string coding * / String STR = "coding check whether the UTF8" ; // verifying that the string is the UTF8 Boolean isUTF8 = isEncoding (STR, "the UTF8" ); IF (isUTF8) { System.out.println ( "the string is. 8-UTF" ); } else { System.out.println ( "the string is not. 8-UTF" ); } } }
Code results are as follows: