解析.csv文件時,我遍歷文件的列標題并查看其中一個是否等于(忽略大小寫)比較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()計算結果為falsewhile 兩個字符串都有 value id。觀察調試器顯示該header值是一個非緊湊字符串 (UTF-16),而該comparand值是一個緊湊字符串(ASCII):這是默認行為還是錯誤?我怎樣才能使equalsIgnoreCase評估達到true預期?
添加回答
舉報
0/150
提交
取消