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

前端 未结 23 2773
我在风中等你
我在风中等你 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:44

    I know this is a very-very old question and answer has also been accepted. But still I would like to submit a very simple answer to original question. Consider this code:

    String str = "Hello-World:How\nAre You&doing";
    inputs = str.split("(?!^)\\b");
    for (int i=0; i

    OUTPUT:

    a[0] = "Hello"
    a[1] = "-"
    a[2] = "World"
    a[3] = ":"
    a[4] = "How"
    a[5] = "
    "
    a[6] = "Are"
    a[7] = " "
    a[8] = "You"
    a[9] = "&"
    a[10] = "doing"
    

    I am just using word boundary \b to delimit the words except when it is start of text.

提交回复
热议问题