Mihae Kheel
Mihae Kheel

Reputation: 2651

(Java) get String value after second to the last forward slash and last forward slash

I need to get a certain string value after a slash "/" but I am having trouble because the format value of the string is not always the same.

The value of string are the ff:

String raw = "Announcements/announcement_id)";

All I need here is the announcement_id which can be obtain with the use of lastIndexOf("/")+1 but this is not always the format of the string it could be also like this.

String raw = "Announcements/announcement_id/Comments/comment_id)";

Here all I need are both announcement_id and comment_id.

For the last part string value could be also like this.

String raw = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)";

Here all I need are both comment_id and reply_id.

How do I get this right like using if else statement.

if(has only one slash)
   use lastIndexOf("/")+1 to get the id

else if (has three or more slashes)
   get the value after second to the last forward slash and last forward slash

Upvotes: 0

Views: 1381

Answers (4)

Azad
Azad

Reputation: 171

The following method is for more paths

String s = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)";
String[] paths = s.split("/");

if (paths.length % 2 == 0) {
  if (paths.length == 2) {
    System.out.println(paths[1]);
  } else {
    int lastPos = paths.length - 1;
    int beforeLastPos = lastPos - 2;
    System.out.println(paths[beforeLastPos] + " " + paths[lastPos]);
  }
}

Upvotes: 0

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

You can use this regex, which allows optional parts in string and appropriately captures announcement_id, comment_id and reply_id

(?:Announcements/([^/]*))(?:/Comments/([^/]*))?(?:/Replies/([^/]*))?\)

Each of which you can access by group1, group2 or group3 respectively.

And as each part is made optional, hence it allows you to optionally have reply_id without having commend_id and as per your samples, I have made announcement_id as mandatory even which you can make it optional.

Demo

Check out this sample Java code,

List<String> list = Arrays.asList("Announcements/announcement_id)",
        "Announcements/announcement_id/Comments/commend_id)",
        "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)");

Pattern p = Pattern.compile("(?:Announcements/([^/]*))(?:/Comments/([^/]*))?(?:/Replies/([^/]*))?\\)");

list.forEach(x -> {
    Matcher m = p.matcher(x);
    if (m.matches()) {
        System.out.println("For string '" + x + "' --> announcement_id: " + m.group(1) + ", commend_id: "
                + m.group(2) + ", reply_id: " + m.group(3));
    }
});

Which prints the data appropriately as found,

For string 'Announcements/announcement_id)' --> announcement_id: announcement_id, commend_id: null, reply_id: null
For string 'Announcements/announcement_id/Comments/commend_id)' --> announcement_id: announcement_id, commend_id: commend_id, reply_id: null
For string 'Announcements/announcement_id/Comments/comment_id/Replies/reply_id)' --> announcement_id: announcement_id, commend_id: comment_id, reply_id: reply_id

Upvotes: 4

XtremeBaumer
XtremeBaumer

Reputation: 6435

String s = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id";
String[] h = s.split("\\/");
switch (h.length) {
case 2:
    System.out.println(h[1]);
    break;
case 4:
    System.out.println(h[1] + " " + h[3]);
    break;
case 6:
    System.out.println(h[3] + " " + h[5]);
    break;
default:
    break;

}

This code also provides a way on how to continue after the split, as well as access the elements you need

Upvotes: 2

Aruna Karunarathna
Aruna Karunarathna

Reputation: 1021

Your order of pathParams are predifined, so use split and parse the array.

 String s = "Announcements/announcement_id/Comments/comment_id/Replies/reply_id)";
 String [] pathParams = s.split("/");

Upvotes: 1

Related Questions