问题

我有一个字符串:

/abc/def/ghfj.doc

我想从中提取ghfj.doc,即last/之后的子字符串,或者右边的first/

有人可以提供一些帮助吗?


#1 热门回答(224 赞)

String example = "/abc/def/ghfj.doc";
    System.out.println(example.substring(example.lastIndexOf("/") + 1));

#2 热门回答(35 赞)

一个非常简单的实现String.split()

String path = "/abc/def/ghfj.doc";
// Split path into segments
String segments[] = path.split("/");
// Grab the last segment
String document = segments[segments.length - 1];

#3 热门回答(30 赞)

你试过什么?这很简单:

String s = "/abc/def/ghfj.doc";
s.substring(s.lastIndexOf("/") + 1)

原文链接