How to prevent java.lang.String.split() from creating a leading empty string?

后端 未结 9 1375
天命终不由人
天命终不由人 2020-11-27 05:50

passing 0 as a limit argument prevents trailing empty strings, but how does one prevent leading empty strings?

for instance

String[] test =          


        
9条回答
  •  一个人的身影
    2020-11-27 06:13

    When using JDK8 and streams, just add a skip(1) after the split. Following sniped decodes a (very wired) hex encoded string.

    Arrays.asList("\\x42\\x41\\x53\\x45\\x36\\x34".split("\\\\x"))
        .stream()
        .skip(1) // <- ignore the first empty element
        .map(c->""+(char)Integer.parseInt(c, 16))
        .collect(Collectors.joining())
    

提交回复
热议问题