打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
字符串一样,但是equals为false?

前言

有时候写代码会遇到一些莫名其妙的问题,两个字符串明明toString()打印一模一样,但是equals就是为false。

问题

直接看代码

public static void main(String[] args) { String s1 = 'hello‌​world‌'; String s2 = 'helloworld'; System.out.println(s1.equals(s2));}

这代码应该够简单了,毫无疑问输出true啊。但是我们还是实际操作一下:


这是什么情况,居然输出了false,我多年的java白学了吗?

结论

其实这个问题很简单,因为字符串s1中包含了不可打印字符,可以把两个字符串复制到 QQ/TIM 看一下就知道了,或者直接按F12审查元素也可以看到。或者我们继续在Java代码中查看

public static void main(String[] args) {    String s1 = 'hello‌​world‌';    String s2 = 'helloworld';    System.out.println(s1.equals(s2));    System.out.println(Arrays.toString(s1.getBytes()));    System.out.println(Arrays.toString(s2.getBytes()));}


这下应该很清楚的知道了为什么两个字符串toString()看起来一样,但是equals却为false。

不可见字符从哪来

说一个最常见的场景,window下新建一个test.txt文件(用window自带的记事本),随便写点什么,就“helloworld”吧。保存(另存为)的时候选择UTF-8编码。


这种方式保存的文件,window会在文件头部添加一个字符,叫做BOM(byte-order mark,字节顺序标记)以UTF-8编码时是三个字节,分别是EF BB BF,用来标记这是一个UTF8编码的文件。程序读取文件时,会把BOM头一起读入内存:

public static void compareContent() { InputStream is = null; try { is = new FileInputStream(new File('D:\\test.txt')); byte[] buff = new byte[16]; int nRead = 0; StringBuilder sb = new StringBuilder(); while ((nRead = is.read(buff)) != -1) { sb.append(new String(buff, 0, nRead)); } //UTF-8文件读取的字符串 String fileStr = sb.toString(); //程序定义的字符串,此处不包含不可打印字符 String localStr = 'helloworld'; System.out.println(fileStr.toString()); System.out.println(localStr.toString()); System.out.println('文件字符串:' + Arrays.toString(fileStr.getBytes())); System.out.println('本地字符串:' + Arrays.toString(localStr.getBytes())); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }

运行结果如下:


除了前三个字节,后面的内容其实是一模一样的。

解决办法

如果文件非要以UTF-8编码保存,可以有以下几种方法:

  • 保存的时候去掉BOM头(notepad++支持以UTF-8无BOM格式编码
  • 程序做兼容,兼容代码如下:
    public static String deleteUTF8Bom(String fileStr) {    byte[] UTF8_BOM_BYTES = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};    byte[] bytes = fileStr.getBytes();    if (bytes[0] == UTF8_BOM_BYTES[0]             && bytes[1] == UTF8_BOM_BYTES[1]             && bytes[2] == UTF8_BOM_BYTES[2]) {         return new String(bytes, 3, bytes.length - 3);     }    return fileStr;}

总结

这个问题不太容易发现,但是其实也是属于基础内容。也说明眼见不一定为实,看到的字符串不一定就是真正的字符串。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
java字符串的各种编码转换类ChangeCharset
一个 Java 字符串到底有多少个字符?
Go 学习笔记(31)— 字符串 string、字符 rune、字节 byte、UTF
Delphi与字符编码
【Net】StreamWriter.Write 的一点注意事项
去除bom
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服