1

I am receiving a string from server trailing one or two lines of spaces like below given string.

String str = "abc*******         
          ********";

Consider * as spaces after my string

i have tried a few methods like

str = str.trim();

str = str.replace(String.valueOf((char) 160), " ").trim();

str = str.replaceAll("\u00A0", "");

but none is working. Why i am not able to remove the space?

5
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a minimal reproducible example. Use the "edit" link to improve your question - do not add more information via comments. Thanks! Commented Jul 28, 2017 at 7:30
  • 1
    Rest assured: trim() is working. But we cant help to spot the problem in your code on such input. In other words: provide a real minimal reproducible example that allows us to repro the problem. Commented Jul 28, 2017 at 7:31
  • Are the character really spaces? Or some special characters? And is that a linebreak inside your string? Commented Jul 28, 2017 at 7:31
  • i have edited my question @f1sh that is a string receiving from server side Commented Jul 28, 2017 at 7:34
  • @sonam, we would really need a better idea of what the actual string is, rather than one where you have replaced characters with other arbitrary asterisk characters. Commented Jul 28, 2017 at 13:33

4 Answers 4

2

You should try like this:

str = str.replaceAll("\n", "").trim();

You can observe there is a new line in that string . first replace new line "\n" with space("") and than trim

Sign up to request clarification or add additional context in comments.

8 Comments

I do not understand. The documentation clearly states that trim should remove any character less than the decimal value 20 in Unicode. \n has a decimal representation of 10, therefore trim should remove it and this should not help. Also, and maybe this is a pointless quibble, but "" is not space, " " is. "" is empty.
i know but as per her question i understand there was a new line ,so trim was not working there
'\n' is a newline. trim should always remove newlines.
yeh but it is not working ,thats why this is a alternate situation
I guess I have two different points to make. Firstly, if someone told you (for example) "I have a variable x which is equal to 5, then I type int y = x;, but then when I print y, it prints out 7, what am I doing wrong?", it would not be logical to tell them "Try this: int y = (int)(((double)x) / 15 * (7 + 8) - 2 - 3 + 5);". By the definition of the language, those are functionally equal, and in the same way, it doesn't make sense that removing newlines first could have an effect on trailing whitespace if you are calling trim anyway.
|
0

You should do:

str = str.replaceAll("\n", "");

In my case use to work the function trim()

Comments

0

Try this: str = str.replaceAll("[.]*[\\s\t]+$", "");

Comments

0

I have tried your 3 methods, and them all work. I think your question describing not correctly or complete, in fact, a String in java would not like

String str = "abc*******         
      ********";

They must like

String str = "abc*******"         
      + "********";

So I think you should describe your question better to get help.

1 Comment

Wouldn't this be better served as a comment on the question? It doesn't really provide an answer.

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.