Get Substring from a String in Java

Captai-N :

I have the following text:

...,Niedersachsen,NOT IN CHARGE SINCE: 03.2009, CATEGORY:...,

Now I want to extract the date after NOT IN CHARGE SINCE: until the comma. So i need only 03.2009 as result in my substring.

So how can I handle that?

String substr = "not in charge since:";
String before = s.substring(0, s.indexOf(substr));
String after = s.substring(s.indexOf(substr),s.lastIndexOf(","));

EDIT

for (String s : split) {
    s = s.toLowerCase();
    if (s.contains("ex peps")) {
        String substr = "not in charge since:";
        String before = s.substring(0, s.indexOf(substr));
        String after = s.substring(s.indexOf(substr), s.lastIndexOf(","));

        System.out.println(before);
        System.out.println(after);
        System.out.println("PEP!!!");
    } else {
        System.out.println("Line ok");
    }
}

But that is not the result I want.

YCF_L :

You can use Patterns for example :

String str = "Niedersachsen,NOT IN CHARGE SINCE: 03.2009, CATEGORY";
Pattern p = Pattern.compile("\\d{2}\\.\\d{4}");
Matcher m = p.matcher(str);

if (m.find()) {
    System.out.println(m.group());
}

Output

03.2009

Note : if you want to get similar dates in all your String you can use while instead of if.


Edit

Or you can use :

String str = "Niedersachsen,NOT IN CHARGE SINCE: 03.03.2009, CATEGORY";
Pattern p = Pattern.compile("SINCE:(.*?)\\,");
Matcher m = p.matcher(str);

if (m.find()) {
    System.out.println(m.group(1).trim());
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=473080&siteId=1