如何获取uri的最后一个路径段 [英] How to obtain the last path segment of an uri

查看:142
本文介绍了如何获取uri的最后一个路径段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入的字符串是 URI 。怎么可能得到最后一个路径段?
,在我的情况下是一个id?

I have in input a string that is an URI. how is 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 for sure there is 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆