使用Java怎么將中文字符串與unicode進行轉(zhuǎn)換

這篇文章給大家介紹使用Java怎么將中文字符串與unicode進行轉(zhuǎn)換,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名申請、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、廬陽網(wǎng)站維護、網(wǎng)站推廣。

Java是什么

Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。

/**
 * 中文字符串和unicode互轉(zhuǎn)工具類 <br>
 * 
 * @author hkb <br>
 */
public class UnicodeConvertUtils {

  /**
   * 實現(xiàn)js的escape函數(shù)
   * 
   * @param input
   *      待傳入字符串
   * @return
   */
  public static String escape(String input) {
    int len = input.length();
    int i;
    char j;
    StringBuffer result = new StringBuffer();
    result.ensureCapacity(len * 6);
    for (i = 0; i < len; i++) {
      j = input.charAt(i);
      if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) {
        result.append(j);
      } else if (j < 256) {
        result.append("%");
        if (j < 16) {
          result.append("0");
        }
        result.append(Integer.toString(j, 16));
      } else {
        result.append("%u");
        result.append(Integer.toString(j, 16));
      }
    }
    return result.toString();

  }

  /**
   * 實現(xiàn)js的unescape函數(shù)
   * 
   * @param input
   *      待傳入字符串
   * @return
   */
  public static String unescape(String input) {
    int len = input.length();
    StringBuffer result = new StringBuffer();
    result.ensureCapacity(len);
    int lastPos = 0, pos = 0;
    char ch;
    while (lastPos < len) {
      pos = input.indexOf("%", lastPos);
      if (pos == lastPos) {
        if (input.charAt(pos + 1) == 'u') {
          ch = (char) Integer.parseInt(input.substring(pos + 2, pos + 6), 16);
          result.append(ch);
          lastPos = pos + 6;
        } else {
          ch = (char) Integer.parseInt(input.substring(pos + 1, pos + 3), 16);
          result.append(ch);
          lastPos = pos + 3;
        }
      } else {
        if (pos == -1) {
          result.append(input.substring(lastPos));
          lastPos = len;
        } else {
          result.append(input.substring(lastPos, pos));
          lastPos = pos;
        }
      }
    }
    return result.toString();
  }

  /**
   * unicode轉(zhuǎn)中文
   * 
   * @param input
   *      待傳入字符串
   * @return
   */
  public static String toGb2312(String input) {
    input = input.trim().replaceAll("(?i)\\\\u", "%u");
    return unescape(input);
  }

  /**
   * 中文字符串轉(zhuǎn)unicode
   * 
   * @param input
   *      待傳入字符串
   * @return
   */
  public static String toUnicode(String input) {
    input = input.trim();
    String output = escape(input).toLowerCase().replace("%u", "\\u");
    return output.replaceAll("(?i)%7b", "{").replaceAll("(?i)%7d", "}").replaceAll("(?i)%3a", ":")
        .replaceAll("(?i)%2c", ",").replaceAll("(?i)%27", "'").replaceAll("(?i)%22", "\"")
        .replaceAll("(?i)%5b", "[").replaceAll("(?i)%5d", "]").replaceAll("(?i)%3D", "=")
        .replaceAll("(?i)%20", " ").replaceAll("(?i)%3E", ">").replaceAll("(?i)%3C", "<")
        .replaceAll("(?i)%3F", "?").replaceAll("(?i)%5c", "\\");
  }

  /**
   * 測試
   * 
   * @param args
   */
  public static void main(String[] args) {
    System.out.println(toUnicode("你好"));
    System.out.println(toGb2312("\u4f60\u597d"));
    // 等同于上面
    System.out.println(toGb2312("\\u4f60\\u597d"));
  }
}

關(guān)于使用Java怎么將中文字符串與unicode進行轉(zhuǎn)換就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)站標題:使用Java怎么將中文字符串與unicode進行轉(zhuǎn)換
URL分享:http://bm7419.com/article22/jddsjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、電子商務(wù)靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司