Let's assume I have a String that contains data in square brackets. How to split content of this String using a specific word. Below I show simplified example of what I would like to do. My string:
"[Chapter 1]
[Lorem ipsum 123]
[Lorem ipsum 456]
Lorem ipsum 789
[Chapter 2]
[Lorem ipsum 012]
[Lorem ipsum 345]"
My attempt:
public void method() {
String myString = "[Chapter 1][Lorem ipsum 123][Lorem ipsum 456]Lorem ipsum 789[Chapter 2][Lorem ipsum 012][Lorem ipsum 345]";
String []myArray = myString.split("\[Chapter"); for (String var : myArray) {
System.out.println(var);
}
}
I want to split my String every time when "[Chapter" is present. Results of splitting:
First String:
"[Chapter 1]
[Lorem ipsum 123]
[Lorem ipsum 456]
Lorem ipsum 789"
Second String:
"[Chapter 2]
[Lorem ipsum 789]
[Lorem ipsum 012]"