将两个字符串组合成表示路径的单个字符串

use*_*949 3 java

我想把字符串"test/""/go"as 组合起来"test/go".

我怎样才能做到这一点?

dan*_*cek 11

java.io.File使用最简单的方法是:

String combined_path = new File("test/", "/go").getPath();
Run Code Online (Sandbox Code Playgroud)


les*_*ana 6

FilenameUtils.normalize()来自Apache Commons IO做你想要的.

例:

FilenameUtils.normalize("foo/" + "/bar");
Run Code Online (Sandbox Code Playgroud)

返回字符串 "foo/bar"


Bab*_*bar 5

作为建议的乔恩斯基特 这里

public static String combine (String path1, String path2)
{
    File file1 = new File(path1);
    File file2 = new File(file1, path2);
    return file2.getPath();
}
Run Code Online (Sandbox Code Playgroud)