java接收特殊字符_Java获取字符的Unicode编码以及如何过滤特殊字符ZWNJ

本文展示了如何使用Java获取字符的Unicode编码,包括十进制和十六进制表示,并讨论了如何过滤掉特殊字符ZWNJ,以避免在数据库中存储为乱码。通过实例代码演示了从XML文件读取内容并替换ZWNJ的过程。
摘要由CSDN通过智能技术生成

获取Unicode编码

package com.xs.test;

public class Test {

public static void main(String[] args) throws Exception {

int decimal = ((int)'中');

System.out.println(decimal); // Unicode十进制编码

String hex = Integer.toHexString(decimal);

System.out.println(hex); // Unicode十六进制编码

System.out.println("中".contains("\u4e2d"));

}

}输出结果:

20013

4e2d

true

过滤特殊字符ZWNJ(zero-width non-joiner)

字符ZWNJ是个不可见的特殊字符,在数据库中往往会被保存为乱码,所以要过滤掉。

文件a.xml包含两个特殊字符ZWNJ:

中华‌人民‌共和国过滤掉字符ZWNJ:

package com.xs.test;

import java.io.File;

import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class Test2 {

public static void main(String[] args) throws IOException {

String path = Test2.class.getResource("a.xml").getFile();

String content = FileUtils.readFileToString(new File(path), "UTF-8");

System.out.println(content);

char c1 = content.charAt(content.indexOf('华') + 1);

int unicode1 = (int)c1;

String hexUnicode1 = Integer.toHexString(unicode1);

System.out.println(hexUnicode1);

char c2 = content.charAt(content.indexOf('民') + 1);

int unicode2 = (int)c2;

String hexUnicode2 = Integer.toHexString(unicode2);

System.out.println(hexUnicode2);

System.out.println("-------------过滤后------------");

System.out.println(content.replaceAll("\u200c", ""));

}

}输出结果:

中华?人民?共和国

200c

200c

-------------过滤后------------

中华人民共和国

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值