0

I try to split some String by the byte value. Like "first\x00second" by 0x00 splitter. I found that compiler cannot combine \x token with variable.

static public ArrayList split_by_byte(String value, byte spliter) {

if (spliter < 0)
throw new IllegalArgumentException("Отрицательное значение разделителя: " + spliter);

ArrayList<String> result = new ArrayList();

String[] groups = value.split("[\\x" + spliter + "]");

for (String group : groups) {

result.add(group);
}

return result;
}

How can i use variable value in patterns like \xNN?

Yury Finchenko
  • 1,035
  • 13
  • 19
  • Your `spliter` variable is assigned using its base10 value, maybe try converting it to its hex string equivalent – AguThadeus Jul 27 '17 at 05:43
  • Just convert the `spliter` [value from hex to char](https://stackoverflow.com/questions/4785654/convert-a-string-of-hex-into-ascii-in-java). – Wiktor Stribiżew Jul 27 '17 at 06:29

1 Answers1

1

In regex you cannot use \x in a single-quoted / non-interpolated string. It must be seen by the lexer.

because tilde isn’t a meta-character.

Add use regex "debug" and you will see what is actually happening.

you can also use pattern and matcher classes and split method...

Mayank Sharma
  • 403
  • 2
  • 2