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.
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
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 ]