aspose file tools*
The moose likes Java in General and the fly likes Not able to trim() a String which contains two paragraphs. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to trim() a String which contains two paragraphs." Watch "Not able to trim() a String which contains two paragraphs." New topic
Author

Not able to trim() a String which contains two paragraphs.

venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
I am parsing a file using dom parser. I have an element <content> which contains two paragraphs of data. I have assigned this to a string. As there is space between two paragraphs I tried to remove the empty spaces using string.trim(). It is not working. Can someone help me how to comeout of this and remove the empty spaces between two paragraphs.


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

trim() only removes the spaces at the beginning and end of a string. It does not remove spaces that are in the middle of a string.

What exactly do you want; do you want to replace multiple spaces with a single space throughout the string? You can do that with a regular expression.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Carina Caoor
Greenhorn

Joined: Jan 21, 2013
Posts: 7
You can use

venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
If I apply , I am getting the output as
Hi guys how are you doing. As a matter of. of fact this is true.
. Could you please look onto it.
venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
"Hi guys how are you doing. As a matter of. of fact this is true. "
This is what I am getting as I placed it in quote it removed all the empty spaces. More over the second para is not being displayed.
"I have a doubt over this. Is it fine?"
Carina Caoor
Greenhorn

Joined: Jan 21, 2013
Posts: 7
Can you print out what is the string in the 'content' outside the loop..




Also once you get the complete string from the loop, try to replace the characters outside the loop.
venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
I placed the sop statement outside the loop as below


I am getting the display as below
Carina Caoor
Greenhorn

Joined: Jan 21, 2013
Posts: 7
Please try to print this outside the loop

venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
It is displaying the following

Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

venkatPrasad Regula wrote:It is displaying the following



Put two "\n" like "\\n\\n" or "\n\n" and try again. I think it should work fine.


OCPJP
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

Ishan Pandya wrote:
venkatPrasad Regula wrote:It is displaying the following



Put two "\n" like "\\n\\n" or "\n\n" and try again. I think it should work fine.


like this content.replaceAll("\n\n", "\n");
venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
Ishan,
I am getting the following as output
venkatPrasad Regula
Greenhorn

Joined: Sep 28, 2010
Posts: 28
This is the code I have changed
Carina Caoor
Greenhorn

Joined: Jan 21, 2013
Posts: 7
Can you write how exactly you want the text to be printed.


As far as I am getting it as





output:

Hi guys how are you doing. As a matter of
of fact this is true.
I have a doubt over this. Is it fine?


Carina Caoor
Greenhorn

Joined: Jan 21, 2013
Posts: 7
Else you can use String TOkenizer to print in one line




output:

Hi guys how are you doing. As a matter of of fact this is true. I have a doubt over this. Is it fine?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to trim() a String which contains two paragraphs.