Results 1 to 11 of 11
- 04-20-2009, 09:29 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
[SOLVED] remove all white space from text file
Hi all
I'm new to this forum and pretty new to java in general.
I've got a question. Does anyone know how to remove all white space from a text file and i don't mean duplicate white space, i mean actually remove all spaces so there are none at all?
I hope that makes sense
Thanks
loki
- 04-20-2009, 09:42 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
what exactly do you need to know for this task? how to read/write a file? how to remove whitespace from a string? i'm sure you can google both and get answers quickly.
- 04-20-2009, 09:46 PM #3
If you read in a line from a text file you use the trim() method to remove leading and trailing white space.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 04-20-2009, 09:57 PM #4
Check out RandomAccessFile... There is a setLength() method...
Who Cares... As Long As It Works...
- 04-21-2009, 04:23 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can do it in two ways. But the second way is not encourage to use.
Case 1:
Using regular expressions. Leading and trailing white spaces can be deleted using trim() method of the String class. To remove spaces in between you can use a regular expression.
Case 2:
Using StringTokernizer. This is straight forward. Tokernize the string based on space character. Then append each element in to the previous element and build the final string object.
- 04-21-2009, 02:55 PM #6
If I recall correctly, you can use the replaceAll method in the String class.
i.e myString.replaceAll(" ", "");
- 04-21-2009, 05:03 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
it might be more efficient to use a whitespace character in multiples "\\s+" or at least i think that is the right regex (never been good at it)
- 04-21-2009, 05:39 PM #8
yeah, that might work better, I've always found regular expressions to be highly irregular and genrally painful in the frontal lobe area. lol
- 04-22-2009, 05:30 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually regular expressions are more powerful, and easy to use, if you know how to use it correctly. And also, it's not easy to learn about regular expressions in a short period of time. But spending more time is not wasting at all.
- 04-23-2009, 05:00 PM #10
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
Hey guys thanks for the replies it's much appreciated!
I googled regular expressions like some of you said and found some useful info.
I tried to post the link to a website that i found, but the forum wouldn't let me as i've posted less than 20 posts.
Thanks again for the help
loki
- 04-26-2009, 11:52 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
NP lol :) Just workout and see, how regular expressions are work and how far it's easy to work on.
Similar Threads
-
remove variables/line in a text file
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-04-2009, 03:05 AM -
How to remove 2 last lines in a text file?
By Marius in forum New To JavaReplies: 2Last Post: 11-30-2008, 03:54 PM -
Remove duplicate lines from a text file
By Dirt.Diver in forum New To JavaReplies: 15Last Post: 06-25-2008, 02:08 PM -
White Space Issue
By sibythoma1984 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-22-2008, 12:05 PM -
Waste Space & Text Field
By Gajesh Tripathi in forum AWT / SwingReplies: 2Last Post: 12-01-2007, 07:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks