Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Replace Special Character with CR and LF  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1775
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a JasperReport(iReport) I need to replace the character"^" in a string with \r\n. It is a field expression.

I have the following but it does not work:

 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, are you sure it's really the character ^? Some editors show unknown characters using a ^; for instance, VI turns \r into ^M.

If you are certain it is ^ you need to replace, you need to look a bit closed at regular expressions. replaceAll takes a regular expression, and ^ is a special character in regular expressions representing the start. Either use replace (with String parameters, added in Java 5 or 6) or escape ^ to "\\^". That's two backslashes - one to escape for use in Java strings.
 
Steve Dyke
Ranch Hand
Posts: 1775
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works great. Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!