如何获取URI的最后一个路径段 [英] How to obtain the last path segment of a URI
本文介绍了如何获取URI的最后一个路径段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我输入的是URI
字符串.
这是我的输入网址:
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屋!
查看全文