Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replace Carriage Returns in String

 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason why the following would not replace the carriage returns and line feeds from a string?

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out backslashes and escapes in regular expressions: http://download.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html#sum
 
Steve Dyke
Ranch Hand
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1619
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I found the solution "[\r\n]+"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic