zoukankan      html  css  js  c++  java
  • java String编码转换

    1. /** 
    2.     * Get XML String of utf-8 
    3.     *  
    4.     * @return XML-Formed string 
    5.     */  
    6.     public static String getUTF8XMLString(String xml) {  
    7.     // A StringBuffer Object  
    8.     StringBuffer sb = new StringBuffer();  
    9.     sb.append(xml);  
    10.     String xmString = "";  
    11.     String xmlUTF8="";  
    12.     try {  
    13.     xmString = new String(sb.toString().getBytes("UTF-8"));  
    14.     xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");  
    15.     System.out.println("utf-8 编码:" + xmlUTF8) ;  
    16.     } catch (UnsupportedEncodingException e) {  
    17.     // TODO Auto-generated catch block  
    18.     e.printStackTrace();  
    19.     }  
    20.     // return to String Formed  
    21.     return xmlUTF8;  
    22.     } 

     /**
      * 字符串编码转换的实现方法
      * @param str  待转换编码的字符串
      * @param oldCharset 原编码
      * @param newCharset 目标编码
      * @return
      * @throws UnsupportedEncodingException
      */
     public String changeCharset(String str, String oldCharset, String newCharset)
       throws UnsupportedEncodingException {
      if (str != null) {
       //用旧的字符编码解码字符串。解码可能会出现异常。
       byte[] bs = str.getBytes(oldCharset);
       //用新的字符编码生成字符串
       return new String(bs, newCharset);
      }
      return null;
     }

  • 相关阅读:
    topcoder srm 320 div1
    topcoder srm 325 div1
    topcoder srm 330 div1
    topcoder srm 335 div1
    topcoder srm 340 div1
    topcoder srm 300 div1
    topcoder srm 305 div1
    topcoder srm 310 div1
    topcoder srm 315 div1
    如何统计iOS产品不同渠道的下载量?
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/5677182.html
Copyright © 2011-2022 走看看