Solved

How to replace a carriage return/line feed in Java

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changePi Challenge 15 76
Eclipse IDE - Cannot copy/paste from console output 8 67
recursion example 16 69
github account with ecipse 1 17
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

17 Experts available now in Live!

Get 1:1 Help Now