CosminO
CosminO

Reputation: 5226

XML preserving leading whitespace, but not trailing whitespaces

Let's say I have this:

<value> Some value</value>

Trying to read the value with:

value=someParent.getElementsByTagName("value").item(0).getTextContent().trim();

gives me "Some value" , but the actual text would be " Some value".

I know trim() leaves out leading and trailing white spaces, but is there an existing method (besides actually adding some lines of code to treat this specific case in particular) in order to have only the trailing white spaces eliminated?

Upvotes: 0

Views: 328

Answers (2)

bowmore
bowmore

Reputation: 11280

Regular expression can help you :

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

the replaceAll() can actually take the place of trim() in your example :

value=someParent.getElementsByTagName("value").item(0).getTextContent().replaceAll("\\s+$", "");

but I would definitly try to make that more readable.

Upvotes: 2

Dominik Sandjaja
Dominik Sandjaja

Reputation: 6466

You can try to use the methods from the Apache commons StringUtils: http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/StringUtils.html

They have strip-methods where you can look at the sourcecode and e.g. adapt the stripEnd method to your needs.

Upvotes: 1

Related Questions