java读取unicode

本文介绍如何使用 Java 处理 Unicode 字符串。提供了两个方法,一个简单版的 `readUnicodeStr` 方法,用于读取纯 Unicode 字符串;另一个 `readUnicodeStr2` 方法增加了对包含英文的 Unicode 字符串的处理。通过示例代码展示如何解析和转换 Unicode 编码,例如:`tbu674eau661fb`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java 读入unicode string 简单版本,示例: \u674e\u661f
<<<<<<<<<<  

public static String  readUnicodeStr(String unicodeStr){
        StringBuilder buf = new StringBuilder();
        //因为java转义和正则转义,所以u要这么写
        String[] cc = unicodeStr.split("\\\\u");
        for (String c : cc) {
            if(c.equals(""))
                continue;
            int cInt = Integer.parseInt(c, 16);
            char cChar = (char)cInt;
            buf.append(cChar);
        }
        return buf.toString();
    }
    
>>>>>>>>>>


java 读入unicode,增加对unicode串中包含的英文的处理,示例:tb\u674ea\u661fb

<<<<<<<<<<<<<

    // tb\u674ea\u661fb
    public static String readUnicodeStr2(String unicodeStr) {
        StringBuilder buf = new StringBuilder();

        for (int i = 0; i < unicodeStr.length(); i++) {
            char char1 = unicodeStr.charAt(i);
            if (char1 == '\\' && isUnicode(unicodeStr, i)) {
                String cStr = unicodeStr.substring(i + 2, i + 6);
                int cInt = Integer.parseInt(cStr,16);
                buf.append((char) cInt);
                // 跨过当前unicode码,因为还有i++,所以这里i加5,而不是6
                i = i + 5;
            } else {
                buf.append(char1);
            }
        }
        return buf.toString();
    }

    // 判断以index从i开始的串,是不是unicode码
    private static boolean isUnicode(String unicodeStr, int i) {
        int len = unicodeStr.length();
        int remain = len - i;
        // unicode码,反斜杠后还有5个字符 uxxxx
        if (remain < 5)
            return false;

        char flag2 = unicodeStr.charAt(i + 1);
        if (flag2 != 'u')
            return false;
        String nextFour = unicodeStr.substring(i + 2, i + 6);
        return isHexStr(nextFour);
    }

    /** hex str 1-9 a-f A-F */
    private static boolean isHexStr(String str) {
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            boolean isHex = ch >= '1' && ch <= '9' || ch >= 'a' && ch <= 'f'
                    || ch => 'A' && ch <= 'F';
            if (!isHex)
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        String output = null;
        
        // tb\u674ea\u661fb
        if(args.length==1)
            output = readUnicodeStr2(args[0]);
        else{
            output = readUnicodeStr(args[1]);
        }
        
        System.out.println(output);
    }
    

>>>>>>>>>>

运行测试:java A 'tb\u674ea\u661fb'
            记着带引号




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值