What is that char
? and how to remove it from a String? I got it from a BufferedReader
and i got it because i read the contents in a char array and this array has to be assigned to a particular size.So, i got the String like that "aaaaaaa "
, and I tried trim
and subString
but didn't change anything:
String a = "aaaaaaa����";
//subString
int i = a.lastIndexOf("a");
a = a.substring(0, i+1);
//trim
a = a.trim();
And this is my way to read the input:
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
char[] a = new char[1000];
int line;
String responseLine, server_response = "";
while((line = in.read(a)) != -1) {
responseLine = String.valueOf(a);
server_response = server_response + responseLine;
}
in.close();
return server_response;
Try with unicode
Unicode corresponding to is \�
String str0 = "aaaaaaa����";
System.out.println(str0.replaceAll("\ufffd", ""));
This is very likely to be an encoding problem; you do not specify the encoding on your InputStreamReader
, as such the system default is used.
Try and use:
new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)
instead.
If you are still stuck with JDK 6, replace StandardCharsets.UTF_8
with Charset.forName("UTF-8")
.
If you are unsure what encoding is used at the other end, you should not use a Reader
but read the contents into a byte array. Then you can use a CharsetDecoder
to try and map the bytes read into one or more encodings.
Example:
StandardCharsets.ASCII.newDecoder()
finally i found a way to solve that, it's not a professional one but efficient enough. all i had to do is filling the char array
with white spaces just before starting the while loop
and then after receiving the whole response i have just to trim it before returning it :
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
char[] a = new char[1000];
int line;
String responseLine, server_response = "";
for(int i = 0; i < a.length; i++){ //
a[i] = ' '; // this is the for loop i added
} //
while((line = in.read(a)) != -1) {
responseLine = String.valueOf(a);
server_response = server_response + responseLine;
for(int i = 0; i < a.length; i++){ //
a[i] = ' '; // this is the for loop i added
} //
}
in.close();
return server_response.trim(); // this is where i return the response trimmed
you could handle it like this:
System.out.println("aaaaaaa����".replace("�", ""));
remaining string will be aaaaaaa
.
I recommend to investigate the input source though and figure out why you get those chars there. Probably there is somewhere an issue with the encoding.
如果您唯一期望的数字和字符,您可以在字节数组上运行for循环,并在每个字符上运行Char.isLetterOrDigit,替换那些不是“”的字符
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.