2

I have a variable text in Java that i managed to read from a calendar blob file. it contain a sample like this:

"BEGIN:VCALENDAR PRODID:-//Kusss//DE VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT UID:at-coursedate-1330089 DTSTAMP:20161110T124248Z DTSTART:20161005T114500Z DTEND:20161005T150000Z SUMMARY:Mobile Computing (367008/2016W)\, Lva-LeiterIn: prof DESCRIPTION: LOCATION:S3 057 X-GWSHOW-AS:BUSY END:VEVENT END:VCALENDAR"

I want to extract specific blocks from the text and store it in an array. my interest is only in getting info of

DTSTART:20161005T114500Z
DTEND:20161005T150000Z

How can i achieve that with simple JAVA Code? thanks so much in advance!

Ani
  • 61
  • 1
  • 7
  • 1
    Note that jQuery is nothing to do with Java. I've removed that tag for you – Rory McCrossan Nov 11 '16 at 10:31
  • seems like a date info, since u managed to extract the whole text just extract the DTstart and dtend by regex matcher and pattern – Ahmad Sanie Nov 11 '16 at 10:36
  • Possible duplicate of [How to find a whole word in a String in java](http://stackoverflow.com/questions/5091057/how-to-find-a-whole-word-in-a-string-in-java) – Ahmad Sanie Nov 11 '16 at 10:36

3 Answers3

0

you will need to delete all backslashes before this works. i would suggest you split the string at every and the iterate through the array and look if the sting startsWith(DTSTART) or startsWith(DTEND) if yes you can store them at some place

String s=new String("BEGIN:VCALENDAR PRODID:-//Kusss//DE VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT UID:at-coursedate-1330089 DTSTAMP:20161110T124248Z DTSTART:20161005T114500Z DTEND:20161005T150000Z SUMMARY:Mobile Computing (367008/2016W), Lva-LeiterIn: prof DESCRIPTION: LOCATION:S3 057 X-GWSHOW-AS:BUSY END:VEVENT END:VCALENDAR");
    String[] test=s.split(" ");
    for(String str:test){
        if(str.startsWith("DTSTART")||str.startsWith("DTEND")){
            System.out.println(str);
        }
    }

produces:

DTSTART:20161005T114500Z
DTEND:20161005T150000Z
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

Or you can just do:

String a = "BEGIN:VCALENDAR PRODID:-//Kusss//DE VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT "
         + "UID:at-coursedate-1330089 DTSTAMP:20161110T124248Z DTSTART:20161005T114500Z "
         + "DTEND:20161005T150000Z SUMMARY:Mobile Computing (367008/2016W)\\, Lva-LeiterIn: "
         + "prof DESCRIPTION: LOCATION:S3 057 X-GWSHOW-AS:BUSY END:VEVENT END:VCALENDAR";

String start = a.substring(a.lastIndexOf("DTSTART"), a.lastIndexOf("DTEND")-1);
String end = a.substring(a.lastIndexOf("DTEND"), a.lastIndexOf("SUMMARY")-1);

System.out.println(start);
System.out.println(end);

would produce:

DTSTART:20161005T114500Z
DTEND:20161005T150000Z
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
0

Using Pattern regex.

Consider you have your text, and the two keywords, you want to do:

String s = "BEGIN:VCALENDAR PRODID:-//Kusss//DE VERSION:2.0 METHOD:PUBLISH BEGIN:VEVENT UID:at-coursedate-1330089 DTSTAMP:20161110T124248Z DTSTART:20161005T114500Z DTEND:20161005T150000Z SUMMARY:Mobile Computing (367008/2016W)\\, Lva-LeiterIn: prof DESCRIPTION: LOCATION:S3 057 X-GWSHOW-AS:BUSY END:VEVENT END:VCALENDAR";       
findValue(s, "DTSTART");
findValue(s, "DTEND");

Send your text, and the desired key to the following method:

private static String findValue(String text, String keyword) {
    Pattern p = Pattern.compile(keyword+ ":([^\\s]+)"); // the regex to be found
    Matcher m = p.matcher(text);
    if (m.find()) { // if found
        return m.group().replace(dtStartOrEnd + ":", ""); // return found, except 'DTSTART:' / 'DTEND:'
    }
    return null; // Not found
}

This method is ready to give you all values within that String, after 'key:' and before the next whitespace.

Jeroen van Dijk-Jun
  • 1,028
  • 10
  • 22