Java prints out the same word, but equals does not.

this question is named goods
Why is it followed by false

Jun.06,2022

your two strings are actually the following a / b array. Because the console is utf-8 encoded, it looks the same

.
char[] a = new char[3];
a[0] = '\uFEFF';
a[1] = '';
a[2] = '';
System.out.println(String.valueOf(a));
char[] b = new char[2];
b[0] = '';
b[1] = '';
System.out.println(String.valueOf(b));

may be invisible characters such as spaces.


2 possibilities

1. There are invisible characters in the data source such as \ r , \ n , \ t and so on
2. The current equals method has been overridden

static void printUTF8Bytes(String s) {
    try {
        byte[] b = s.getBytes("utf-8");
        for(byte c : b) {
            System.out.print(Integer.toHexString(c&0xff));
        }
        System.out.println();
    } catch (java.io.UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

use this printUTF8Bytes method to output the UTF-8 codes of hd and title.


I encountered once that I read out the contents of a txt file and compared it in the code. It was very simple, but in the end, all the lines in it matched. Except for the first line. Finally, debug looked. As a result, the beginning of the first line is inexplicably stuffed with a \ uFEFF

contents of the file

clipboard.png

clipboard.png

char\uFEFF

clipboard.png

\uFEFF

clipboard.png

debug

clipboard.png

so finally, in order to achieve my goal, I abandoned the first line and started reading from the second line, with a space in the first line or whatever I could write, which solved the problem at that time

after Baidu, it seems that the notepad editing tool that comes with windows will add 0xefbbbf (hexadecimal) characters at the beginning of each file when saving UTF-8 files (describe the deployment of javaweb project to tomcat, summary of problems in modifying configuration files )

thx, for reference only

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c550-2c30d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c550-2c30d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?