我在比较字符串时遇到了一个奇怪的问题。我从客户端向我的服务器发送一个字符串(作为字节使用getBytes()
)。我通过以-Dfile.encoding=UTF-8
.
当我尝试对valueOf
从客户端收到的字符串执行 a 以将其转换为枚举时,我注意到了这个问题。当我打印出字符串时,它们看起来完全一样。但是当我执行 a 时compareTo
,我得到一个非零数字并equals
返回false
。
我假设这是一个编码问题。不过我不太确定——当谈到使用套接字进行客户端-服务器编程时,我还是个新手。
这就是我得到的:
Waiting for connections on port 9090
Connected to client: 127.0.0.1
received command: GetAllItems
The value is |GetAllItems| (from client)
The value is |GetAllItems| (from enum)
equals: false
我究竟做错了什么?
更新
这是我从流中重构字符串的方式。也许这是我做错了什么?
byte[] commandBytes = new byte[1024];
in.read(commandBytes); //in is a BufferedInputReader
String command = new String(commandBytes);