6

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.

7
  • 2
    String.substring is case-sensitive, so in your example, substr won't be found in s. (stackoverflow.com/questions/1126227/indexof-case-sensitive) Commented May 17, 2017 at 12:14
  • Have edit my post Commented May 17, 2017 at 12:16
  • 1
    @Captai-N you should probably consider a regex. It would be much more efficient, and readable. Commented May 17, 2017 at 12:17
  • Your issue is using lastIndexOf(","). If the string after SINCE contains more than one ,, it won't work. You should get the first index of , in the substring after SINCE Commented May 17, 2017 at 12:18
  • 2
    @Shashwat Well. If there are no other colons in that string, then a simple indexOf(':') will beat any regex regarding performance. Commented May 17, 2017 at 12:21

6 Answers 6

8

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());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion it works as far. But the date can also be a full date format for example 01.03.20009
4

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(','));

Comments

4

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!

1 Comment

Makes sense ... but his question isnt really clear. Does he want the number, or a string with the trailing space ;-)
1

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.