1

I have a very long string in my Android project, so I split it across multiple lines:

<string name="my_str">
    AAAAAAAAAAA
    BBBBBBBBBBB
</string>

When I use this string, I get AAAAAAAAAAA BBBBBBBBBBB. Is there a way to get rid of the space in the middle? (I want AAAAAAAAAAABBBBBBBBBBB)

Obviously I could put the entire string on one line, but that's not very maintainable.

Edit: The string (AAA...BBB) can contain spaces; I only want to remove the spaces created by the newline in the XML.

1
  • what if you break with shift+enter? Commented Jan 11, 2017 at 20:58

4 Answers 4

1

To remove all whitespaces and non-visible characters (e.g. tab, newline,..):

mystring.replaceAll("\\s+", "");

or to remove new-lines

mystring.replaceAll("\n","");
Sign up to request clarification or add additional context in comments.

Comments

1

So remove all white spaces from beginning and from end:

myString = myString.replaceAll("^\\s+","").replaceAll("\\s+$","");

Check out the documentation: ^ is for beginning, $ for end. \s means any white space like tabs, spaces, etc.

Comments

0
myString = myString.replaceAll(" ","");

2 Comments

Sorry if my question wasn't clear, I only want to remove the extra spaces created by the newline in the XML.
Yes, probably I understood your question wrongly. I paid attention to this part: "I get AAAAAAAAAAA BBBBBBBBBBB. Is there a way to get rid of the space in the middle? (I want AAAAAAAAAAABBBBBBBBBBB )" so I am sorry for inappropriate answer. To remove newline symbol you can use "\n" instead of " ".
0

Remove white space in string array It's very simple to remove from white space in string array. for example:- String rahul = 12, 13, 14, 15, 16

Solution :

rahul.trim().replaceAll("\\s","");

output is : 12,13,14,15,16

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.