How to replace a carriage return/line feed in Java

Hi, I am trying to replace a carriage return\line feed befeo I enter a string in a sql ststement.

My code is as follows

      if ((((String)requestData.get("emailTemplateSubjectText")).indexOf("\n")) > 0){
        String removedCarriageReturnsText = "";
        while((((String)requestData.get("emailTemplateSubjectText")).indexOf("\n")) > 0) {
          System.err.println("Line break exists in text");
          removedCarriageReturnsText = (String)requestData.get("emailTemplateSubjectText");
          System.err.println("removedCarriageReturnsText = " + removedCarriageReturnsText);
          removedCarriageReturnsText = this.replaceSubstring(removedCarriageReturnsText,"\n","");
          System.err.println("removedCarriageReturnsText2 = " + removedCarriageReturnsText);
          requestData.put("emailTemplateSubjectText", removedCarriageReturnsText);

where replaceSubstring is the following function

  private String replaceSubstring(String original, String existingSubString, String newSubString) {
    StringBuffer buffer = new StringBuffer("");

    buffer.append(original.substring(0, original.indexOf(existingSubString)));
    buffer.append(original.substring(original.indexOf(existingSubString) + existingSubString.length(), original.length()));

    return buffer.toString();

However when I do this it does not replace the carriage returns as i require. I know the replacesubstring function works as i have used this in other classes, however I cannot seemt o get it to replace my carriage returns.

Do I need to replace this with the ascii value, and if so then how do I go about this. If not this, then how do I solve my problem?

Who is Participating?
girionisConnect With a Mentor Commented:
First of all I have to tell you thatthe carriage return is represented by the '\r' character sequence.

  Second if you are using jdk1.4 there is already a method in the String class that is called replace and does exactly this; replaces a character with another. Take a look, it might come in handy.

  Hope it helps.
Jim CakalicSenior Developer/ArchitectCommented:
The String.replace method will only substitute one character for one other character as many times as the target character appears in the String. It cannot handle multi-character replacements nor can it remove characters from the String. I've found minimal usefulness in the replace method.

Your problem is that the carriage return/line feed sequence is two characters: "\r\n". If you want to eliminate these from the String then you will want to change your replaceSubstring call to account for both of these characters.

Best regards,
Jim Cakalic
seoirseAuthor Commented:
Cheers for the help. Jim your answer also would have solved my problem also, but it was the \r that I needed to check for that I had forgotten about (as you mentioned also).
Cheers to both of you.
 Thank you for the points, I am glad I helped :-) (even though I think jim_cakalic is worth half the points as well).
All Courses

From novice to tech pro — start learning today.