This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes Remove all control characters except \n & \r Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove all control characters except \n & \r" Watch "Remove all control characters except \n & \r" New topic

Remove all control characters except \n & \r

Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

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

Joined: Oct 27, 2005
Posts: 20193

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".

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Remove all control characters except \n & \r
It's not a secret anymore!