File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove all control characters except \n & \r

 
Srikanth Madasu
Ranch Hand
Posts: 48
Eclipse IDE Hibernate Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement of removing all the control characters from a string except CarriageReturn (CR) and LineFeed (LF).

I know how to remove all the control characters:



But i now want retain CR & LF and remove all other control characters.
Any help is much appreciated.

Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API of Pattern already tells you how to do this - if you know where to look. It's in the "Character classes" section: [a-z&&[^bc]]. In this case, it's [\\p{Cntrl}&&[^\r\n]]

Keep in mind that the results are... odd. The \r only returns the cursor position to the start of the same line on Windows, so this is the output:
As you see, the "hello" is overwriting the start of the line. At first, it will overwrite the "A" and part of the tab. After the pattern replacing, it will replace "A Thi".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic