Solved

How to replace a carriage return/line feed in Java

Posted on 2002-06-06
4
16,723 Views
Last Modified: 2012-08-13
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(newSubString);
    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?

0
Comment
Question by:seoirse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 90 total points
ID: 7059204
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.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7059245
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
0
 

Author Comment

by:seoirse
ID: 7059348
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.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7059360
 Thank you for the points, I am glad I helped :-) (even though I think jim_cakalic is worth half the points as well).
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question