简体   繁体   English

我如何摆脱“空白”

[英]How can I get rid of “white spaces”

I'm writing server-client project. 我正在编写服务器-客户端项目。 The server side is being developed in Java, and the client side in Python. 服务器端用Java开发,客户端用Python开发。 Recently when I try to send a string over the socket, the server gets it with white spaces in it. 最近,当我尝试通过套接字发送字符串时,服务器在其中获取了带有空格的字符串。 Let's say I send: 假设我发送:

1:user:password

in the server side I use a string split to convert this one string into 3, the first string (in this case "1") will tell the server what to do with the rest (the username and the password). 在服务器端,我使用字符串拆分将此字符串转换为3,第一个字符串(在本例中为“ 1”)将告诉服务器如何处理其余的字符串(用户名和密码)。 Now the problem is that when I print what I get from the socket in the server side I get something like this: 现在的问题是,当我打印从服务器端套接字获得的内容时,会得到如下信息:

1 : u s e r : p a s s w o r d 

with white spaces between every character. 每个字符之间都有空格。 I tried to use the String function replaceAll("\\\\s","") and replaceAll("\\\\s+","") but they didn't seem to work. 我尝试使用String函数replaceAll("\\\\s","")replaceAll("\\\\s+","")但它们似乎没有用。 The I used replaceAll("\\\\S","k") with a capital S, and realized that it actually showed me: 我将replaceAll("\\\\S","k")与大写S一起使用,并意识到它实际上向我显示了:

kkkkkkkkkkkkkkkkkkkkkkkkkkkkk

So I got to the conclusion that those "white spaces" aren't really white spaces, because the \\\\S replaces "anything that isn't a space character (including both letters and numbers, as well as punctuation etc)". 因此,我得出的结论是,那些“空格”并不是真正的空格,因为\\\\S代替了“不是空格字符的任何内容(包括字母和数字以及标点符号等)”。

My question is: What are those blanks, and how can I get rid of them? 我的问题是:这些空白是什么,我如何摆脱它们?

Thanks in advance. 提前致谢。 Sorry for the long post. 抱歉,很长的帖子。

Most likely you're sending UTF-16 (probably UTF-16LE) but decoding it as UTF-8. 您很可能发送了UTF-16(可能是UTF-16LE),但将其解码为UTF-8。 The "spaces" are actually nulls (zero bytes). “空格”实际上是空值(零字节)。 You need to use the same character encoding on the client and the server. 您需要在客户端和服务器上使用相同的字符编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM