public class Main {
public static void main(String[] args) {
String path = "example/path/to/file";
String result = getLastPathSegment(path);
System.out.println(result); // 输出: file
}
/**
* 截取最后一个斜杠后的字符串
* @param path 输入的路径字符串
* @return 最后一个斜杠后的字符串,如果没有斜杠则返回原字符串
*/
public static String getLastPathSegment(String path) {
if (path == null || path.isEmpty()) {
return "";
}
int lastSlashIndex = path.lastIndexOf('/');
if (lastSlashIndex != -1) {
return path.substring(lastSlashIndex + 1);
} else {
return path;
}
}
}
下一篇:java @component
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站