两个相同的字符串不相等(不是指针/引用错误) [英] Two identical Strings are not equal(Not pointer/reference mistake)

查看:205
本文介绍了两个相同的字符串不相等(不是指针/引用错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文件中读取一行:

KatalogObrazków132

意味着我应该在以下位置查找数据:

Means that I should look for data in:

C:\Users\NAME_OF_THE_USER/KatalogObrazków

所以我这样做,但有可怕的事情发生。在 splitLine [0] 我有一个单词KatalogObrazków然后计算机说 KatalogObrazków.equals(splitLine [0])为false,分割线后没有空格 splitLine [0] 。请看下面的代码。

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. 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();

输出:

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

编辑:

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 


推荐答案

您可能在文件开头遇到过UTF-BOM。

You may have encountered the UTF-BOM at the beginning of your file.

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

它是隐形的,因为大多数编辑都隐藏它。很邪恶,是吗?

It's invisible because most editors hide it. Pretty evil, huh?

这篇关于两个相同的字符串不相等(不是指针/引用错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆