• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use

 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please try to print this outside the loop

 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is displaying the following

 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ishan,
I am getting the following as output
 
venkatPrasad Regula
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic