In my opinion, this sort of problem is usually best to use regular expression. While using a combination of replace substring indexOf can work but it can be difficult for the next dev to understand the real logic.
This is the regular expression solution:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class testmain {
public static void main(String[] args) {
String someString = "FAIL: some random message with ID_temptemptemp and with original ID_1234567890";
Pattern pattern3 = Pattern.compile("ID_(\\d+)");
Matcher matcher3 = pattern3.matcher(someString);
String s = null;
while (matcher3.find()) {
s = matcher3.group(1); // Keep overriding until the last set of captured value
}
System.out.println(s);
}
}
Sample output:
1234567890
The expression ID_(\\d+) means that we are looking for occurrances of the word "ID_" and if matched, capture the remaining digits.
The following while loop is just to go through all the captured patterns and keep overriding the s variable with the captures until the last one, which fits your requirement.
The original problem:
The issue with your initial solution was that after you did a .split(...) the length of the splited value in position [1] string is no longer the same as the original string value, and hence you should be doing lastIndexOf(someString.split("FAIL: ")[1]) instead to compare.
Therefore giving you the output ID_1234567890
Example:
System.out.println(someString.split("FAIL: ")[1].substring(someString.split("FAIL: ")[1].lastIndexOf("ID_")));
The remaining code works just fine.
Tips:
Tips on debugging, maybe get an IDE like IntelliJ and step through the code to see what the code is doing on each step. This would give you a better idea.