我输入了一个 URI
字符串。如何获得最后一个路径段(在我的情况下是一个 id)?
这是我的输入网址:
String uri = "http://base_path/some_segment/id"
我必须获得我尝试过的 id:
String strId = "http://base_path/some_segment/id";
strId = strId.replace(path);
strId = strId.replaceAll("/", "");
Integer id = new Integer(strId);
return id.intValue();
但它不起作用,肯定有更好的方法来做到这一点。
最佳答案
这就是你要找的东西:
URI uri = new URI("http://example.com/foo/bar/42?param=true");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
int id = Integer.parseInt(idStr);
或者
URI uri = new URI("http://example.com/foo/bar/42?param=true");
String[] segments = uri.getPath().split("/");
String idStr = segments[segments.length-1];
int id = Integer.parseInt(idStr);
关于java - 如何获取 URI 的最后一个路径段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4050087/