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

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

    An extremely naive and inefficient solution which works nevertheless.Use split twice on the string and then concatenate the two arrays

    String temp[]=str.split("\\W");
    String temp2[]=str.split("\\w||\\s");
    int i=0;
    for(String string:temp)
    System.out.println(string);
    String temp3[]=new String[temp.length-1];
    for(String string:temp2)
    {
            System.out.println(string);
            if((string.equals("")!=true)&&(string.equals("\\s")!=true))
            {
                    temp3[i]=string;
                    i++;
            }
    //      System.out.println(temp.length);
    //      System.out.println(temp2.length);
    }
    System.out.println(temp3.length);
    String[] temp4=new String[temp.length+temp3.length];
    int j=0;
    for(i=0;i

提交回复
热议问题