zoukankan      html  css  js  c++  java
  • 如何判断一段字符的编码格式

     1 public class TranCharset {
     2     /**
     3      * 判断字符串的编码
     4      *
     5      * @param str
     6      * @return
     7      */
     8     public static String getEncoding(String str) {
     9         String encode = "GB2312";
    10         try {
    11             if (str.equals(new String(str.getBytes(encode), encode))) {
    12                 String s = encode;
    13                 return s;
    14             }
    15         } catch (Exception exception) {
    16         }
    17         encode = "ISO-8859-1";
    18         try {
    19             if (str.equals(new String(str.getBytes(encode), encode))) {
    20                 String s1 = encode;
    21                 return s1;
    22             }
    23         } catch (Exception exception1) {
    24         }
    25         encode = "UTF-8";
    26         try {
    27             if (str.equals(new String(str.getBytes(encode), encode))) {
    28                 String s2 = encode;
    29                 return s2;
    30             }
    31         } catch (Exception exception2) {
    32         }
    33         encode = "GBK";
    34         try {
    35             if (str.equals(new String(str.getBytes(encode), encode))) {
    36                 String s3 = encode;
    37                 return s3;
    38             }
    39         } catch (Exception exception3) {
    40         }
    41         return "";
    42     }
    43     public static void main(String[] args){
    44         System.out.println(getEncoding("CSS测试"));
    45     }
    46 
    47 }
  • 相关阅读:
    01
    py5.30
    py 5.28
    py5.25
    py 5.24
    py 5.22
    py5.21
    py 5.18
    py 5.17
    py 5.16
  • 原文地址:https://www.cnblogs.com/xiayahui/p/4996741.html
Copyright © 2011-2022 走看看