Split string by byte

I have a String in which there are some ASCII control characters (namely RS (0x1e) and US (0x1f)). I defined them in my code as such:

 static public final byte RS = 0x1E; static public final byte US = 0x1F; 

later in my code, I want to break the string using these characters:

 String[] records = content.split(String.valueOf(RS)); 

but it does not work correctly. After some attempts, I found that it

 String[] records = content.split("\u001e"); 

works, but in this case I have to remember the codes. I use static RS byte also in other parts, so just changing this parameter is not real. I could, of course, create RS_STRING or something else, but that means double work.

Any clean good solution for this?

+8
java string split
source share
2 answers

Declaring a character as char rather than byte fixed for me - the following works fine:

 char RS = 0x1E; String s = new String(new char[]{'d', RS, 'e'}); System.out.println(s.split(String.valueOf(RS)).length); //Prints 2 

However, using a byte as a type results in an error:

 byte RS = 0x1E; String s = new String(new char[]{'d', (char)RS, 'e'}); System.out.println(s.split(String.valueOf(RS)).length); //Prints 1 

Of course, you can return a char in a byte if you need to reference it as such in other parts of your code.

+5
source share

The problem is using String.valueOf() , because there is no String.valueOf (bytes).

Instead, the byte is gradually expanded to int and String.valueOf(int) . And this method returns a decimal string representation of int.

That's why the suggestion of declaring RS as a char corrects it, String.valueOf (char) really does what you expect (provides a String with that char in it).

You can alternately add the explicit cast when converting to the String representation: String.valueOf((char) RS) and save the commercial RS declared as byte.

+2
source share

All Articles