如何获取uri的最后一个path段
我在input一个string是一个URI 。 怎么可能得到最后的path段? 在我的情况是一个ID? 
这是我的inputurl
 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); 
这是一个简短的方法来做到这一点:
 public static String getLastBitFromUrl(final String url){ // return url.replaceFirst("[^?]*/(.*?)(?:\\?.*)","$1);" <-- incorrect return url.replaceFirst(".*/([^/?]+).*", "$1"); } 
testing代码:
 public static void main(final String[] args){ System.out.println(getLastBitFromUrl( "http://example.com/foo/bar/42?param=true")); System.out.println(getLastBitFromUrl("http://example.com/foo")); System.out.println(getLastBitFromUrl("http://example.com/bar/")); } 
输出:
42
FOO
酒吧
说明:
 .*/ // find anything up to the last / character ([^/?]+) // find (and capture) all following characters up to the next / or ? // the + makes sure that at least 1 character is matched .* // find all following characters $1 // this variable references the saved second group from above // Ie the entire string is replaces with just the portion // captured by the parentheses above 
 import android.net.Uri; Uri uri = Uri.parse("http://example.com/foo/bar/42?param=true"); String token = uri.getLastPathSegment(); 
 我知道这是旧的,但这里的解决scheme似乎比较冗长。 如果您有一个URL或URI只需简单易读的一行: 
 String filename = new File(url.getPath()).getName(); 
 或者如果你有一个String : 
 String filename = new File(new URL(url).getPath()).getName(); 
如果您使用的是Java 8,并且您希望文件path中的最后一个段可以执行。
 Path path = Paths.get("example/path/to/file"); String lastSegment = path.getFileName().toString(); 
 如果你有一个url,比如http://base_path/some_segment/id你可以做。 
 final Path urlPath = Paths.get("http://base_path/some_segment/id"); final Path lastSegment = urlPath.getName(urlPath.getNameCount() - 1); 
 在Java 7+中,可以组合一些以前的答案来允许从URI中检索任何path段,而不仅仅是最后一个段。 我们可以将URI转换为java.nio.file.Path对象,以利用其getName(int)方法。 
 不幸的是,静态工厂Paths.get(uri)不是为了处理httpscheme而build立的,所以我们首先需要将scheme从URI的path中分离出来。 
 URI uri = URI.create("http://base_path/some_segment/id"); Path path = Paths.get(uri.getPath()); String last = path.getFileName().toString(); String secondToLast = path.getName(path.getNameCount() - 2).toString(); 
要在一行代码中获得最后一个段,只需将上面的行嵌套。
 Paths.get(URI.create("http://base_path/some_segment/id").getPath()).getFileName().toString() 
 要获得倒数第二个段,同时避免索引号和潜在的错误,使用getParent()方法。 
 String secondToLast = path.getParent().getFileName().toString(); 
 请注意,可以重复调用getParent()方法以相反的顺序检索段。 在这个例子中,path只包含两个段,否则调用getParent().getParent()将检索倒数第三个段。 
 您可以使用getPathSegments()函数。  ( Android文档 ) 
考虑你的例子URI:
 String uri = "http://base_path/some_segment/id" 
您可以使用以下方法获取最后一个段
 List<String> pathSegments = uri.getPathSegments(); String lastSegment = pathSegments.get(pathSegments.size - 1); 
  lastSegment将是id 。 
从URI获取URL并使用getFile(),如果你不准备使用子string解压缩文件的方式。
 如果您的项目中包含commons-io ,则可以使用org.apache.commons.io.FilenameUtils创build不必要的对象 
 String uri = "http://base_path/some_segment/id"; String fileName = FilenameUtils.getName(uri); System.out.println(fileName); 
 会给你的path的最后一部分,这是id