For something like this you really should use a proper XML parsing library, but if you have to do it like this, then simply using a split here would be unnecessarily complex as splitting is meant for, well, literally splitting a string by a delimiter which wouldn't work because you have two delimiters here (<OrderInformation>
and </OrderInformation>
).
You'd want to do something like this:
public static String extract(String input, String start, String end) {
int length = end.length();
int startIndex = input.indexOf(start);
// because indexOf returns index of the first character found,
// we need to add the length of the "end" string to get the index of the last character.
int endIndex = input.indexOf(end) + length;
return input.substring(startIndex, endIndex);
}
String result = extract(in, "<OrderInformation>", "</OrderInformation>")
You can read more about substring()
here.
Keep in mind that this solution won't work if there's multiple occurrences of <OrderInformation>
. You would need to search for multiple occurrences manually and return some sort of an array.