简体   繁体   中英

Get Substring from a String in Java

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.

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());
}

You can use : to separate the String s.

String substr = "NOT IN CHARGE SINCE:";
String before = s.substring(0, s.indexOf(substr)+1);
String after = s.substring(s.indexOf(':')+1, s.lastIndexOf(','));

Of course, regular expressions give you more ways to do searching/matching, but assuming that the ":" is the key thing you are looking for (and it shows up exactly once in that position) then:

s.substring(s.indexOf(':')+1, s.lastIndexOf(',')).trim();

is the "most simple" and "least overhead" way of fetching that substring.

Hint: as you are searching for a single character, use a character as search pattern; not a string!

If you have a more generic usecase and you know the structure of the text to be matched well you might profit from using regular expressions:

Pattern pattern = Pattern.compile(".*NOT IN CHARGE SINCE: \([0-9.]*\),");
Matcher matcher = pattern.matcher(line);
System.out.println(matcher.group());

A more generic way to solve your problem is to use Regex to match Every group Between : and ,

Pattern pattern = Pattern.compile("(?<=:)(.*?)(?=,)");
Matcher m = p.matcher(str);

You have to create a pattern for it. Try this as a simple regex starting point, and feel free to improvise on it:

String s = "...,Niedersachsen,NOT IN CHARGE SINCE: 03.2009, CATEGORY:....,";
Pattern pattern = Pattern.compile(".*NOT IN CHARGE SINCE: ([\\d\\.]*).*");
Matcher matcher = pattern.matcher(s);

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

That should get you whatever group of digits you received as date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM