This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Differentiating between newline escape characters and carriage returns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Differentiating between newline escape characters and carriage returns" Watch "Differentiating between newline escape characters and carriage returns" New topic
Author

Differentiating between newline escape characters and carriage returns

Abdullah Akbar
Greenhorn

Joined: May 03, 2008
Posts: 2

Dear team,

I am trying to write code for reading files with text content. The problem is that the string values which I have contain both new line escape characters as well as carriage returns. I need to get my strings rid of the carriage returns only and not the new line escape characters. Could some one please help me out here?

System.out.println("START- " + dataString + " -END");
System.out.println();
System.out.println("START- " + dataString.replaceAll("\\n","") + " -END");
System.out.println();
System.out.println("START- " + dataString.replaceAll("\\r", "") + " -END"); --- doesn't work

The output is:


START- Configure synchronization and upload of all processes or specific process
Configure synchronization and upload of all processes or specific process -END

START- Configure synchronization and upload of all processes or specific process Configure synchronization and upload of all processes or specific process -END

START- Configure synchronization and upload of all processes or specific process
Configure synchronization and upload of all processes or specific process -END
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Don't escape the \. \r has special meaning (carriage return) in both Java and in regular expressions.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37952
    
  22
. . . and welcome to the Ranch
Abdullah Akbar
Greenhorn

Joined: May 03, 2008
Posts: 2

Thank you very much Rob and Campbell.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Differentiating between newline escape characters and carriage returns
 
Similar Threads
Skipping a blank line
Definition of carriage return and line feed
Assigning to char, giving problem
Unicode charector as String literal; finalize() method
Illegal character ??