How to split a string, but also keep the delimiters?

前端 未结 23 2327
我在风中等你
我在风中等你 2020-11-21 06:32

I have a multiline string which is delimited by a set of different delimiters:

(Text1)(DelimiterA)(Text2)(DelimiterC)(Text3)(DelimiterB)(Text4)
23条回答
  •  囚心锁ツ
    2020-11-21 06:56

    I don't think it is possible with String#split, but you can use a StringTokenizer, though that won't allow you to define your delimiter as a regex, but only as a class of single-digit characters:

    new StringTokenizer("Hello, world. Hi!", ",.!", true); // true for returnDelims
    

提交回复
热议问题