Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove space and blank lines from String

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a string with a blank line created when the user hit the enter key twice..

"Type this first

Hit enter twice and I am here".

How do I compress this extra blank line so it looks like

"Type this first
Hit enter twice and I am here"

Thanks for any help,
Jehan
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a brief code example demonstrating your problem?
[ October 03, 2006: Message edited by: sven studde ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will give you the tips!

The space character that creates a line between the two sentences is "\n"

In other words, your sentence is composed like this:

Type this first\n
\n
Hit enter twice and here I am

Therefore what you have to do is to replace the two "\n" with a single one, so that the sentece is:

Type this first\n
Hit enter twice and here I am

One options could be to use the replace() method in the String class, another option could be to use the delete() or deleteCharAt() methods of StringBuffer or StringBuilder.

Good luck!
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
I will give you the tips!

The space character that creates a line between the two sentences is "\n"


Here's a tip: not for the most popular operating system in the world!
[ October 03, 2006: Message edited by: sven studde ]
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention that I am using java 1.1.

Basically what I have to do is compress all the blank lines of a string up so that there are no blank lines. So for example if the user enters..

"This is line one.

I press enter twice now here."

It should be

""This is line one.
I press enter twice now here."

If I can get a code sample, that would be great.

Thanks again,
Jehan
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you required to use Java 1.1? If you can use later versions, the problem is easy to resolve using regular expressions.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to do some research on the topic of "newlines". Newlines are invisible characters in your String that create blank lines when the String is output. Just like regular characters, the invisible newline characters can be removed from a String. So, your problem is almost the same as trying to remove all the d's from the String. The difficulty is that the invisible character(s) that make up a newline are different on different operating systems.

Windows: \r\n
Mac: \r
Unix: \n

So, you have to come up with a solution that works for all three operating systems.

In addition, you don't want to remove all the newlines. If you did that, your String would just be one line of text.
[ October 05, 2006: Message edited by: sven studde ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic