如何获取 URI 的最后一个路径段 [英] How to obtain the last path segment of a URI
本文介绍了如何获取 URI 的最后一个路径段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我输入了一个 URI
字符串作为输入.怎么可能得到最后一个路径段(在我的例子中是一个 id)?
I have as input a string that is a URI
. how is it possible to get the last path segment (that in my case is an id)?
这是我的输入网址:
String uri = "http://base_path/some_segment/id"
我必须获得我尝试过的id:
and I have to obtain the id I have tried with this:
String strId = "http://base_path/some_segment/id";
strId = strId.replace(path);
strId = strId.replaceAll("/", "");
Integer id = new Integer(strId);
return id.intValue();
但它不起作用,肯定有更好的方法来做到这一点.
but it doesn't work, and surely there must be a better way to do it.
推荐答案
就是你要找的:
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);
这篇关于如何获取 URI 的最后一个路径段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文