有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (6) 个答案

  1. # 1 楼答案

    仅使用^{}最简单的方法是:

    String combined_path = new File("test/", "/go").getPath();
    
  2. # 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();
    }
    
  3. # 3 楼答案

    来自Apache Commons IO^{}做你想做的事

    例如:

    FilenameUtils.normalize("foo/" + "/bar");
    

    返回字符串"foo/bar"

  4. # 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


    这个解决方案

    • 不限于文件路径,而是任何类型的字符串
    • 不会替换令牌中的任何字符,只会从边界修剪它们
  5. # 5 楼答案

    附加这两个字符串,并将//替换为/,如下所示

    "test//go".replace("//", "/")
    

    输出:test/go

  6. # 6 楼答案

    String test =  "test/";
    String go = "/go";
    String result = test + go.substring(1);