Solved

How to replace a carriage return/line feed in Java

Posted on 2002-06-06
4
16,519 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Requested array size exceeds VM limit 3 84
java.io.BufferedReader   readLine()  //returns the entire file ? 4 59
diffSum example 4 23
Windows 10 IE Certificate Issue 10 41
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now