This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Replace Carriage Returns in String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Replace Carriage Returns in String" Watch "Replace Carriage Returns in String" New topic
Author

Replace Carriage Returns in String

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1438
    
    1
Is there a reason why the following would not replace the carriage returns and line feeds from a string?

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

Without working out exactly what's happening there...I think you need to escape the \s in the Strings.

Try .replaceAll("\\r\\n", "^") etc.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Check out backslashes and escapes in regular expressions: http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#sum


Correlation does not prove causality.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1438
    
    1
Roger F. Gay wrote:Check out backslashes and escapes in regular expressions: http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#sum


Thanks, I have looked at this link yet Nothing I try seems to fix my issue.

Is there a way to tell exactly what character was used to cause a string to be broken into two lines?

What is strange is I have the replaceAll("\r\n","") code on any string run before storing it in my database. Yet there is a string in one of the records(case in point) that is broken. Seems like somthing was used besides the "r" or "n".
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
I haven't done a lot of this type of string operations in Java. I've done a bunch of regex in JavaScript. What I'm not sure about is:

1. why you getString and such when replaceAll() is a String method ... i.e. why not rs.replaceAll() ? Does getString from a string make a copy that you're not saving anywhere (for example)?

2. What's with the "^" ... replace with anything?
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1438
    
    1
Roger F. Gay wrote:I haven't done a lot of this type of string operations in Java. I've done a bunch of regex in JavaScript. What I'm not sure about is:

1. why you getString and such when replaceAll() is a String method ... i.e. why not rs.replaceAll() ? Does getString from a string make a copy that you're not saving anywhere (for example)?

2. What's with the "^" ... replace with anything?


1. I suppose the getString() is overkill.

2. The "^" is just a subsitution for the original string carriage returns and line feeds. The user types in formated data ito a textarea. The sql connection uploads the string after the carriage returns and line feeds are replaced by the "^". Upon retreival the string is read by AJAX and passed to the js which replaces the "^" with the "\r\n". In the case this original post is concerned the "\r\n" did not work as expected.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Steve Dyke wrote:2. The "^" is just a subsitution for the original string carriage returns and line feeds. The user types in formated data ito a textarea. The sql connection uploads the string after the carriage returns and line feeds are replaced by the "^". Upon retreival the string is read by AJAX and passed to the js which replaces the "^" with the "\r\n". In the case this original post is concerned the "\r\n" did not work as expected.


But "^" is a regex operator. I'm wondering if that's making it wacky.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

Steve Dyke wrote:What is strange is I have the replaceAll("\r\n","") code on any string run before storing it in my database. Yet there is a string in one of the records(case in point) that is broken. Seems like somthing was used besides the "r" or "n".

Is there are chance that it's just using a \n instead of \r\n (e.g. Unix line-endings instead of Windows ones)? Your code will only strip them when they occur together. Strip them out separately and see if that works.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1438
    
    1
Matthew Brown wrote:
Steve Dyke wrote:What is strange is I have the replaceAll("\r\n","") code on any string run before storing it in my database. Yet there is a string in one of the records(case in point) that is broken. Seems like somthing was used besides the "r" or "n".

Is there are chance that it's just using a \n instead of \r\n (e.g. Unix line-endings instead of Windows ones)? Your code will only strip them when they occur together. Strip them out separately and see if that works.


I am using the following code on this string and nothing I am trying will remove the line break

DETAIL P/N GTR01C14) CONFORMS
CPN ELECT CERT PO #137722 / GLENAIR LOT #O938. (LOT # LISTED ON WALLACE C of C IS: 1029) NO TRACABLITY


Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1438
    
    1
I think I found the solution "[\r\n]+"
 
 
subject: Replace Carriage Returns in String