简体   繁体   中英

Java: How to parse a String?

How to get last path path_X ?

/some/random/path_1/
/some/random/path_2

Thus, it is necessary to use 3rd party libraries like Guava or Apache Commons .

尝试这个:

final String last = Paths.get("/some/random/path_1/").getFileName().toString();

我相信这也可以,是吗?

String fileName = new File(path).getName();

Another approach would be

String s = "/some/random/path_1/";
String[] sa = s.split("/");

String last = sa[sa.length-1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM