1

I have a txt file like this:

P 4 0 3 0 2 0 4 0 6 P 3 0 2 7 4 5 8 S 2 3 4 5 4 T 3 4 5 6 7 8 3

I read it into a string, and was trying to split the string. I need to split the String at P, S, T and store the string into an arraylist. I had this so far

List<String> list = new ArrayList<String>(Arrays.asList(content.split("P")));

        for (int i = 0; i < list.size(); i++)
        {
            System.out.println(list.get(i));
        }

and got

ArrayList size: 3
4 0 3 0 2 0 4 0 6 
 3 0 2 7 4 5 8 S 2 3 4 5 4 T 3 4 5 6 7 8 3

while i want the strings in the ArrayList to be something like

P 4 0 3 0 2 0 4 0 6 
P 3 0 2 7 4 5 8 
S 2 3 4 5 4 
T 3 4 5 6 7 8 3

With each line representing a string. How can i go about doing something like this?

xingbin
  • 27,410
  • 9
  • 53
  • 103
WagzZz
  • 47
  • 1
  • 5
  • This might help https://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters – Sam Apr 30 '18 at 06:43
  • @Sam Cheers! I didnt know what to search for, was searching for 'splitter' but I guess it was meant to be delimiter. Thanks :) – WagzZz Apr 30 '18 at 06:46

1 Answers1

1

Use regex lookbehind to split on the empty string "" followed by a character P|S|T :

content.split("(?=[PST])");
xingbin
  • 27,410
  • 9
  • 53
  • 103