如何在 Java 中执行与编码无关的字符串比较?
我在比较字符串时遇到一个奇怪的问题。我从客户端向服务器发送一个字符串(使用 getBytes()
作为字节)。我通过使用 -Dfile.encoding=UTF-8
启动客户端和服务器,确保客户端和服务器上的编码相同。
当我尝试对从客户端收到的字符串执行 valueOf
并将其转换为枚举时,我注意到了这个问题。当我打印出字符串时,它们看起来完全相同。但是,当我执行 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);
I'm having a strange problem comparing strings. I send a string to my server (as bytes using getBytes()
) from the client. I've ensured that encoding is the same on the client and server by starting both of them with -Dfile.encoding=UTF-8
.
I noticed the problem when I was trying to perform a valueOf
on the string I receive from the client, to convert it into an enum. When I print out the strings, they look exactly the same. But when I perform a compareTo
, I get a non-zero number and equals
returns false
.
I'm assuming that it is an encoding problem. I'm not really sure though -- I'm still a bit of a novice when it comes to client-server programming with sockets.
This is what I get:
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
What am I doing wrong?
UPDATE
Here is how I'm reconstituting the string from the stream. Perhaps this is where I'm doing something wrong?
byte[] commandBytes = new byte[1024];
in.read(commandBytes); //in is a BufferedInputReader
String command = new String(commandBytes);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,由于您的缓冲区比字符串大,因此重构的字符串中添加了空值。在 Java 中将 null 嵌入到字符串中是合法的(与 C 和 Company 不同),尽管 Java 处理它们的方式与标准 UTF-8 不同。
尝试记录读取的长度,并将该长度传递给字符串构造函数:
My guess is that since your buffer is bigger than your string, there are added nulls in the reconsituted string. It is legal for nulls to be embedded inside strings in Java (unlike C and company), although Java handles them differently than standard UTF-8.
Try recording the length read, and pass that length to the string constructor:
您的问题在于如何构造字符串。您正在将字节读入长度为 1024 的缓冲区,但您并没有告诉 String 构造函数仅查看相关点。所以你的代码应该是...
Your problem is in how you are constructing the string. You are reading in the bytes into a buffer length 1024, but you are not telling the String constructor to only look at the relevant points. So your code should be...
使用 java.text.Collator 来比较字符串。
Use
java.text.Collator
to compare strings.