• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remove all occurrencies of carriage return from string

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic