Java 紧凑字符串 equalsIgnoreCase,non-compact 字符串计算结果为 false

Java compact string equalsIgnoreCase with non-compact string evaluates to false

解析 .csv 文件时,我遍历文件的列 headers 并查看其中一个是否等于(忽略大小写)比较项 id:

String comparand = "id";
for (String header : headerMap.keySet()) {
   if (header.equalsIgnoreCase(comparand)) {
      recordMap.put("_id", csvRecord.get(header));
   } else {
      recordMap.put(header, csvRecord.get(header));
   }
}

使用 UTF-8 字符集读取文件:

Reader reader = new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8);

我使用的CSV解析器库是Apache Commons CSV:

CSVParser csvParser = CSVFormat.DEFAULT
   .withDelimiter(delimiter)
   .withFirstRecordAsHeader()
   .withIgnoreEmptyLines()
   .parse(reader);

Map<String, Integer> headerMap = csvParser.getHeaderMap();

上面的 equalsIgnoreCase() 以某种方式求值为 false 而两个字符串的值都是 id.

观察调试器显示 header 值是 non-compact 字符串 (UTF-16) 而 comparand 值是 compact string (ASCII):

这是默认行为还是错误?我怎样才能使 equalsIgnoreCase 像人们期望的那样评估为 true

您的 header 值以 UTF-16 BOM FFFE 开头。在与 comparand.

比较之前读取 header 时去除 BOM