How to obtain the last path segment of a URI

is that what you are looking for:

URI uri = new URI("http://example.com/foo/bar/42?param=true");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf("https://stackoverflow.com/") + 1);
int id = Integer.parseInt(idStr);

alternatively

URI uri = new URI("http://example.com/foo/bar/42?param=true");
String[] segments = uri.getPath().split("https://stackoverflow.com/");
String idStr = segments[segments.length-1];
int id = Integer.parseInt(idStr);

Leave a Comment