1
String output = new String(encryptText);
output = output.replaceAll("\\s", "");
return output;

replaceAll("\\s", ""); doesn't work

3
  • 1
    your code seems to be fine. it should work. I would check what encryptText looks like Commented Apr 11, 2013 at 7:35
  • could you please share the input string? because str.replaceAll("\\s", ""); should work. You can also try str.replaceAll(" ", ""); Commented Apr 11, 2013 at 7:36
  • You should add the error you get or an example of why the solution you posted doesn't work or your question seems to be a duplicate of this one: stackoverflow.com/questions/5455794/… Commented Apr 11, 2013 at 7:36

6 Answers 6

2
String output = new String(encryptText);
output = output.replaceAll(" ", "");
return output;
Sign up to request clarification or add additional context in comments.

1 Comment

We have to say that this solution works only for "pure" whitespace characters. It doesn't catch "special" whitespace characters like tab (\t). Instead, the solution based on java.util.regex.Pattern: replaceAll("\\s", "") does work for every whitespace character: [ \t\n\x0B\f\r]
1

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), "");

Comments

0

You could use the non-regex-version replace to do the job:

output = output.replace(" ", "");

Comments

0

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));

Comments

0

Your code works fine for me, see here

Anyway you can use the StringUtils.trimAllWhitespace from the spring framework:

output = StringUtils.trimAllWhitespace(output);

5 Comments

as I understand this function not from JDK. it come with spring?
it saysThe method trimAllWhitespace(String) is undefined for the type
java.lang does not contain a class called StringUtils. Several third-party libs do, such as Apache Commons Lang or the Spring framework. In order to use it you need the relevant jar in your project classpath and import the correct class.
@AlekseiBulgak yes your right, springsource.org/spring-community-download
Sorry gues, i have forget to mention its part of the spring framework. i updated my answer
0
z = z.replaceAll(" ", "");

easiest and the simplest way for removing white spaces from the char [];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.