Issue
I have a string String str = "start-IDK-2012-2020-end"
I would like to extract "IDK-2012-2020" this alone.
- start- is common for the string -I only want IDk and the year.
other example: String str = "start-IDK-2012-2010-java-substring-sample" expected: IDK-2012-2010
I tried split it dint work
Solution
You can try use String pattern.
Pattern pattern = Pattern.compile("IDK-[0-9]{4}-[0-9]{4}");
String example = "start-IDK-2012-2010-java-substring-sample";
Matcher matcher = pattern.matcher(example);
if(matcher.find()){
String result = example.substring(matcher.start(), matcher.end());
System.out.println(result);
}
Answered By - Gimaz
Answer Checked By - Terry (JavaFixing Volunteer)