[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to replace a carriage return/line feed in Java

Posted on 2002-06-06
4
Medium Priority
?
16,961 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 270 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 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