Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip "control characters" from a CharSequence?

Tags:

java

I have CharSequence source, int start, int end

I would like to strip all "control characters" from source between start and end and return this as a new CharSequence

by "control character" I mean undeseriable characters like Tab and Return, line feeds and such... basically all that was in ASCII < 32 (space) ... but I don't know how to do it in this "modern age"

what is a char? is it unicode? How can I remove these "control characters" ?

like image 367
ycomp Avatar asked Feb 23 '12 16:02

ycomp


1 Answers

You could use CharSequence.subSequence(int, int) and String.replaceAll(String, String) as follows:

source.subSequence(0, start).toString() + source.subSequence(start, end).toString().replaceAll("\\p{Cntrl}", "") + source.subSequence(end, source.length()).toString()
like image 106
Dan Cruz Avatar answered Oct 12 '22 07:10

Dan Cruz