栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

equal()和equalsIgnoreCase()对于相等的字符串返回false

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

equal()和equalsIgnoreCase()对于相等的字符串返回false

确实那将是非常奇怪的:)您可以将上面的代码更改为此:

if ("debug_mode".equalsIgnoreCase("debug_mode"))     debug_mode = true;

确认它工作正常,然后再次检查为什么您

values[0]
不是“ debug_mode”。

这就是我现在要检查的事情清单:

  • 检查一下
    values[0].length() == "debug_mode".length()
  • 我非常怀疑,但是无论如何我还是要把它放在桌子上-您是否有机会使用Unipre?
  • 您可以打印每个字符并
    .equals()
    在该字符和“ debug_mode”字符串的各个字符之间执行操作吗?
  • 如果这是在较大的项目中,则您可以在一个简单的Java项目中执行相同的操作并确认它可以在其中工作吗?

为了澄清,问题实际上是在使用

DataInputStream.readLine
。从javadoc(http://download.oracle.com/javase/1.6.0/docs/api/java/io/DataInputStream.html):

readLine()      Deprecated. This method does not properly convert bytes to characters. ...

它实际上以一种微妙的方式与Unipre有关-当

writeChar
您实际编写两个字节
0
以及
97
字母的大端Unipre时
a

这是一个独立的片段,显示了行为:

import java.io.*;import java.util.*;public class B {  public static void main(String[] args) throws Exception {    String os = "abc";    System.out.println("---- unipre, big-endian");    for(byte b: os.getBytes("UTF-16BE")) {      System.out.println(b);    }    ByteArrayOutputStream baos = new ByteArrayOutputStream();    DataOutputStream dos = new DataOutputStream(baos);    for(char c: os.toCharArray()) {      dos.writeChar(c);    }    byte[] ba = baos.toByteArray();    System.out.println("---- ba");    for(byte b: ba) {      System.out.println(b);    }    ByteArrayInputStream bais = new ByteArrayInputStream(ba);    DataInputStream dis = new DataInputStream(bais);    System.out.println("---- dis");    String s = dis.readLine();    System.out.println(s);    System.out.println("String length is " + s.length()       + ", but you would expect " + os.length()       + ", as that is what you see printed...");  }}

故事的寓意-不要使用不推荐使用的API
…而且,空格是无声的杀手:http : //www.codinghorror.com/blog/2009/11/whitespace-the-
silent-killer.html



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/507838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号