String output = new String(encryptText);
output = output.replaceAll("\\s", "");
return output;
replaceAll("\\s", "");
doesn't work
String output = new String(encryptText);
output = output.replaceAll("\\s", "");
return output;
replaceAll("\\s", "");
doesn't work
String output = new String(encryptText);
output = output.replaceAll(" ", "");
return output;
I was facing the same problem then I searched a lot and found that, it is not a space character but a null value which is transformed into string from character array. This resolved the issue for me -
output.replaceAll(String.valueOf((char) 0), "");
Your code works fine for me, see here
Anyway you can use the StringUtils.trimAllWhitespace from the spring framework:
output = StringUtils.trimAllWhitespace(output);
You could use the non-regex-version replace to do the job:
output = output.replace(" ", "");
Use String.replaceAll(" ","")
or if you want to do it yourself without a lib call, use this.
String encryptedString = "The quick brown fox ";
char[] charArray = encryptedString.toCharArray();
char [] copy = new char[charArray.length];
int counter = 0;
for (char c : charArray)
{
if(c != ' ')
{
copy[counter] = c;
counter++;
}
}
char[] result = Arrays.copyOf(copy, counter);
System.out.println(new String(result));
z = z.replaceAll(" ", "");
easiest and the simplest way for removing white spaces from the char [];