Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Substring from a String in Java

Tags:

java

string

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.

like image 734
Captai-N Avatar asked May 17 '17 12:05

Captai-N


People also ask

How do you substring a string in Java?

The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1 if the second argument is given. Syntax : public String substring(int begIndex, int endIndex) Parameters : beginIndex : the begin index, inclusive. endIndex : the end index, exclusive.

How do you get a substring from a string?

The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string.

How does substring () inside string works?

The substring(int beginIndex, int endIndex) method of the String class. It returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

What does .substring do in Java?

The substring() method returns a substring from the given string. The substring begins with the character at the startIndex and extends to the character at index endIndex - 1 . If the endIndex is not passed, the substring begins with the character at the specified index and extends to the end of the string.


1 Answers

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());
}
like image 72
YCF_L Avatar answered Oct 11 '22 21:10

YCF_L