java - Split a string by a byte -
i have string
has ascii control characters in (namely rs
(0x1e) , us
(0x1f)). have defined them in code such:
static public final byte rs = 0x1e; static public final byte = 0x1f;
later in code, want split string using these characters:
string[] records = content.split(string.valueof(rs));
but, doesn't work correctly. after fiddling found this
string[] records = content.split("\u001e");
does work, in case, have remember codes. use rs
static byte in other parts, changing not real option. of course create rs_string
or something, means double work.
any clean solution this?
declaring character char
rather byte
fixed me - 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 byte type causes fail:
byte rs = 0x1e; string s = new string(new char[]{'d', (char)rs, 'e'}); system.out.println(s.split(string.valueof(rs)).length); //prints 1
you can of course cast char
byte
if need refer such in other parts of code.
Comments
Post a Comment