Jon*_*eet 15
int lastIndex = text.lastIndexOf('/');
String lastPart = text.substring(lastIndex + 1);
Run Code Online (Sandbox Code Playgroud)
(当然还存在其他选项 - /例如正则表达式和分裂......但上面是我要做的.)
请注意,因为我们必须使用它+ 1来越过最后一个/,所以它有一个方便的属性,即使没有任何斜杠它仍然有效:
String text = "no slashes here";
int lastIndex = text.lastIndexOf('/'); // Returns -1
String lastPart = text.substring(lastIndex + 1); // x.substring(0).equals(x)
Run Code Online (Sandbox Code Playgroud)
Per*_*ror 10
"/test/test2/test3".substring("/test/test2/test3".lastIndexOf("/")+1)
Run Code Online (Sandbox Code Playgroud)
还假设它是一个文件路径.你也可以使用File#getname()
File f = new File("/test/test2/test3");
System.out.println(f.getName());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |