• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

remove all occurrencies of carriage return from string

 
neil harrison
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Neil!
What do you mean exactly when you say "field has carriage returns embedded in it"?
 
neil harrison
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic