• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing newline and tabs from a string

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I have a string from which I need to remove all of the newlines, tabs, and spaces. Here is the code I wrote:

It removes the spaces just fine but not the newlines or tabs. How can I do this?

Thanks for the help!
 
Henry Wong
author
Marshal
Pie
Posts: 21496
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brandi Love:
It removes the spaces just fine but not the newlines or tabs. How can I do this?


It works fine for me... can you provide an example of the input string?

Henry
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the input string comes from a TextArea. I'm using getText to get the text from the TextArea. I'm not actually typing the newlines or tabs into the String myself, java places them there from the TextArea. For example...

Here's the code:



I type the following text into inputTextField:
Brandi Love
was here!


and get the following output:
BrandiLove
washere!


It has removed the spaces but not the newline. Very frustrating.
[ February 27, 2007: Message edited by: Brandi Love ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through your text, divide it into a char[] array, then use a for loop to go through the individual characters.
Don't print them out straight, but print them using a %x tag (or %d if you like decimal numbers).Get an ASCII table and look up all the numbers for the characters, and see whether there are any \n or \f or \r. Do this before or after.

Different operating systems use different line terminating characters; this is the first reference I found from Google with "line terminator Linux Windows." It says Windows uses \r\f and Linux \f. You should find that out from your example. Obviously if you strip \n and leave \r you will still have the text break into separate lines.

You might be more successful if you write a regular expression (see this part of the Java Tutorial, etc) which includes whitespace and line terminators, and use it as a delimiter with the String.split() method, then print the individual tokens in order.

Hope this helps. CR
[ February 28, 2007: Message edited by: Campbell Ritchie ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to simply get rid of all white space, you could try

inputString = inputString.replaceAll("\\s", "");
 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked wonderfully Ilja. Thanks for your help, everyone!
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just wondering...

all a tab is, is a combination of spaces, so if you remove all spaces
you remove the tab, and if you just scan the file for lines,

and Lines>1 then you know you have a return, so as you scan just concat
the two or more lines.

to me this would be an easier way of do it, unless the assignment says
specifically that you must detect the \n \t and " ".

Justin
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
I was just wondering...

all a tab is, is a combination of spaces, so if you remove all spaces
you remove the tab, and if you just scan the file for lines,

and Lines>1 then you know you have a return, so as you scan just concat
the two or more lines.

to me this would be an easier way of do it, unless the assignment says
specifically that you must detect the \n \t and " ".

Justin


That doesn't seem any easier than the one-liner Ilja posted. Unless of course the use of a regex engine is prohibited for some reason.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also not true - a tab is not the same as a combination of spaces, even if it ends up having an equivalent effect. A tab is a distinct character in Unicode, and a regex which replaces only spaces will have no effect on a tab. On the other hand, using \s (or "\\s") for whitespace is much more flexible - this is a predefined character class which recognizes multiple forms of whitespace, including standard spaces, and tabs.

On the off chance that the standard definition of whitespace does not apply, you can also construct a custom character class for the occasion. For example, a literal reading of the first post above implies that we should replace newlines, tabs, and spaces - but not returns, form feeds or vertical tabs. (The last two are virtually unused nowadays, but just in case you're dealing with data from 1970 or so...) To replace only those specific characters, you could use something like this:

In most cases using \s is both more concise and more accurate - but if it's not exactly what you need, you can always modify it.
 
Zak Kampmann
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandi Love wrote:Hi all!

I have a string from which I need to remove all of the newlines, tabs, and spaces. Here is the code I wrote:

It removes the spaces just fine but not the newlines or tabs. How can I do this?

Thanks for the help!


The main reason it isn't working is because String.replaceAll(String, String) uses a regex for the first string and not just a pattern. If you change your code to the following:



You should see your expected results.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic