Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes remove all occurrencies of  carriage return from string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "remove all occurrencies of  carriage return from string" Watch "remove all occurrencies of  carriage return from string" New topic
Author

remove all occurrencies of carriage return from string

neil harrison
Greenhorn

Joined: Oct 01, 2003
Posts: 2
I am trying to write information collected from a database to a tab delineated file for opening in excel.
The problem i have is one of the field has carriage returns embedded in it which excel does not like, i am unable to change the code to retrieve the data from the database as that would be the best course.
So how do i search for and remove/replace any carriage returns in a string, any help would make me warm.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Welcome to the JavaRanch, Neil!
What do you mean exactly when you say "field has carriage returns embedded in it"?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
neil harrison
Greenhorn

Joined: Oct 01, 2003
Posts: 2
I am using the following code
for (Enumeration enum = vecMainList.elements(); enum.hasMoreElements()
Opportunity opp = (Opportunity) enum.nextElement();
_fileLine = opp.getAttributeValue("NAME") + "\t" +
opp.getAttributeValue("COMMENTS") + "\n";
_byteLine = _fileLine.getBytes();
_fStream.write(_byteLine);

}
_fileLine = "End of List " + "\n\n";
_byteLine = _fileLine.getBytes();
_fStream.write(_byteLine);
_fStream.close();
response.sendRedirect(_absRptName);

Unfortunately the COMMENTS value has line feeds and carriage returns embedded in it which it inserts into my file, what i would like to do is to remove these so it will write to a file in a single line and not as recovered from the database
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
In J2SDK v1.4 there is a new method in the String class, "replaceAll", that can be used as follows:

The first parameter can be a pattern, but in this instance you just feed it the string that you want to replace. The lengths of the two parameters need not be the same. In this example all carriage-return and line-feed characters will be replaced with an empty string, i.e., they will be removed.
If you are using an earlier version of the JDK, you'll need to write your own method that does this. Just loop through the characters of the string, keeping the ones you want and discarding the undesirable one[s].
[ October 01, 2003: Message edited by: Wayne L Johnson ]
 
 
subject: remove all occurrencies of carriage return from string