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.

YCF_L
  • 49,027
  • 13
  • 75
  • 115
Captai-N
  • 273
  • 2
  • 4
  • 14
  • 2
    `String.substring` is case-sensitive, so in your example, `substr` won't be found in `s`. (http://stackoverflow.com/questions/1126227/indexof-case-sensitive) – Sentry May 17 '17 at 12:14
  • Have edit my post – Captai-N May 17 '17 at 12:16
  • 1
    @Captai-N you should probably consider a regex. It would be much more efficient, and readable. – dumbPotato21 May 17 '17 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` – Jeremy Grand May 17 '17 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. – GhostCat May 17 '17 at 12:21
  • You say that is not the result you want. Can you show *exactly* what is the result you get and which is the one you want? – Sentry May 17 '17 at 12:22
  • @GhostCat well, the OP has `...` as the start of the String, so I implied there could be any character there. If not, you're probably correct. – dumbPotato21 May 17 '17 at 12:25

6 Answers6

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());
}
YCF_L
  • 49,027
  • 13
  • 75
  • 115
  • Thanks for the suggestion it works as far. But the date can also be a full date format for example 01.03.20009 – Captai-N May 17 '17 at 12:25
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(','));
Sky
  • 1,369
  • 1
  • 13
  • 23
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!

GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • 1
    Makes sense ... but his question isnt really clear. Does he want the number, or a string with the trailing space ;-) – GhostCat May 17 '17 at 12:27
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());
Matthias
  • 3,228
  • 2
  • 23
  • 40
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);
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.

Chiranjib
  • 1,549
  • 2
  • 16
  • 28