Question

I had a question about the control characters. I have to found them in a string and delete them. Made some research and found useful tips.

I wrote this:

output.toString().replaceAll("[\\p{Cntrl}\\p{Cc}]","")

But I was asked if this method can find the control characters if they are written in bytes. To be honest, I have no idea. Try to look on the net, but don't know how I can test it.

Thanks

Was it helpful?

Solution

Yes, the characters will be removed, see next code:

byte[] chars = { 'h', 'e', 10, 15, 21, 'l', 'l', 'o', 13 };
String str = new String(chars, "utf8");
System.out.println("==========");
System.out.println(str);
System.out.println("==========");
System.out.println(str.replaceAll("[\\p{Cntrl}\\p{Cc}]", ""));
System.out.println("==========");

The output for that code would be:

 ==========
 he
 llo
 ==========
 hello
 ==========

Once the special character is included in an String object it doesn't matter if was created from a byte[] or whatever else object, It's stored always in the same format.

OTHER TIPS

If by "written in bytes" you mean that your input is a byte array, you can write

String s = new String(myByteArray)

and use your code on s.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top