| Author |
removing newline and tabs from a string
|
Brandi Love
Ranch Hand
Joined: Sep 19, 2003
Posts: 133
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Brandi Love
Ranch Hand
Joined: Sep 19, 2003
Posts: 133
|
|
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
Joined: Oct 13, 2005
Posts: 32599
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
If you want to simply get rid of all white space, you could try inputString = inputString.replaceAll("\\s", "");
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Brandi Love
Ranch Hand
Joined: Sep 19, 2003
Posts: 133
|
|
|
That worked wonderfully Ilja. Thanks for your help, everyone!
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
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
|
You down with OOP? Yeah you know me!
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
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.
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: removing newline and tabs from a string
|
|
|