# 2 楼答案 正如Jon Skeethere所建议的那样 public static String combine (String path1, String path2) { File file1 = new File(path1); File file2 = new File(file1, path2); return file2.getPath(); }
# 4 楼答案 下面是一个Guava方法,在从项目的开头和结尾修剪完此字符的所有实例后,将Iterable<String>项与char项连接起来: public static String joinWithChar(final Iterable<String> items, final char joinChar){ final CharMatcher joinCharMatcher = CharMatcher.is(joinChar); return Joiner.on('/').join( Iterables.transform(items, new Function<String, String>(){ @Override public String apply(final String input){ return joinCharMatcher.trimFrom(input); } })); } 用法: System.out.println(joinWithChar(Arrays.asList("test/", "/go"), '/')); 输出: test/go 这个解决方案 不限于文件路径,而是任何类型的字符串 不会替换令牌中的任何字符,只会从边界修剪它们
# 1 楼答案
仅使用^{} 最简单的方法是:
# 2 楼答案
正如Jon Skeethere所建议的那样
# 3 楼答案
来自Apache Commons IO的^{} 做你想做的事
例如:
返回字符串
"foo/bar"
# 4 楼答案
下面是一个Guava方法,在从项目的开头和结尾修剪完此字符的所有实例后,将
Iterable<String>
项与char
项连接起来:用法:
输出:
这个解决方案
# 5 楼答案
附加这两个字符串,并将
//
替换为/
,如下所示输出:
test/go
# 6 楼答案