Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15208
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carina Caoor
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use

 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I placed the sop statement outside the loop as below


I am getting the display as below
 
Carina Caoor
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try to print this outside the loop

 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is displaying the following

 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan,
I am getting the following as output
 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code I have changed
 
Carina Caoor
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic