简体   繁体   中英

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. 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). 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. The I used replaceAll("\\\\S","k") with a capital S, and realized that it actually showed me:

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)".

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. The "spaces" are actually nulls (zero bytes). You need to use the same character encoding on the client and the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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