简体   繁体   English

两个相同的字符串不相等(不是指针/引用错误)

[英]Two identical Strings are not equal(Not pointer/reference mistake)

I read a line from a file:我从文件中读取了一行:

KatalogObrazków 1 32

Means that I should look for data in:意味着我应该在以下位置查找数据:

C:\Users\NAME_OF_THE_USER/KatalogObrazków

and so I do it, but there is horrible thing going on.所以我这样做了,但发生了可怕的事情。 In splitLine[0] I have a word "KatalogObrazków" but then the computer says that "KatalogObrazków".equals(splitLine[0]) is false, there is no whitespace arround splitLine[0] left after splitting line.splitLine[0]我有一个词"KatalogObrazków"但是计算机说"KatalogObrazków".equals(splitLine[0])是假的,分割线后splitLine[0]没有空格。 Please have a look at the code below.请看下面的代码。

    BufferedReader br = new BufferedReader(new FileReader(path));
    String line;
    String[] splitLine;
    if ((line = br.readLine()) != null) {
        splitLine = line.split(" ");
        System.out.println(splitLine[0]);//1st line of output
        System.out.println("KatalogObrazków".equals(splitLine[0]));//these are not EQUAL!!!!!??? WHY?
        imageDirectoryPath = System.getProperty("user.home")+"/" + splitLine[0];
        System.out.println(new File(imageDirectoryPath).exists());
        delay = Integer.parseInt(splitLine[1]);
        fontSize = Integer.parseInt(splitLine[2]);
    }
    br.close();

Output:输出:

KatalogObrazków
false
false
C:\Users\R/KatalogObrazków

EDIT:编辑:

System.out.println();
            for (char c : splitLine[0].toCharArray())
                System.out.print((int) c + " ");
            System.out.println();
            for (char c : "KatalogObrazków".toCharArray())
                System.out.print((int) c + " ");
            System.out.println();

GOT ME:搞定我:

65279 75 97 116 97 108 111 103 79 98 114 97 122 107 243 119 
75 97 116 97 108 111 103 79 98 114 97 122 107 243 119 

You may have encountered the UTF-BOM at the beginning of your file.您可能在文件开头遇到了 UTF-BOM。

http://en.wikipedia.org/wiki/Byte_order_mark http://en.wikipedia.org/wiki/Byte_order_mark

It's invisible because most editors hide it.它是不可见的,因为大多数编辑器都隐藏了它。 Pretty evil, huh?很邪恶吧?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM