溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Java怎么將中文字符串與unicode進行轉換

發(fā)布時間:2021-02-22 16:19:27 來源:億速云 閱讀:416 作者:Leah 欄目:編程語言

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

Java是什么

Java是一門面向對象編程語言,可以編寫桌面應用程序、Web應用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應用程序。

/**
 * 中文字符串和unicode互轉工具類 <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轉中文
   * 
   * @param input
   *      待傳入字符串
   * @return
   */
  public static String toGb2312(String input) {
    input = input.trim().replaceAll("(?i)\\\\u", "%u");
    return unescape(input);
  }

  /**
   * 中文字符串轉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"));
  }
}

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI