Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Remove all control characters except \n & \r

 
Ranch Hand
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22572
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
reply
    Bookmark Topic Watch Topic
  • New Topic