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;
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句