java 用 File.separator 替换所有“/”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13714722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:44:08  来源:igfitidea点击:

replaceAll "/" with File.separator

javaregexseparator

提问by tomato

In Java, when I do:

在 Java 中,当我这样做时:

    "a/b/c/d".replaceAll("/", "@");

I get back

我回来了

    a@b@c@d

But when I do:

但是当我这样做时:

    "a/b/c/d".replaceAll("/", File.separator);

It throws a StringIndexOutOfBoundsException, and I don't know why. I tried looking this up, but it wasn't very helpful. Can anyone help me out?

它抛出一个 StringIndexOutOfBoundsException,我不知道为什么。我试着查了一下这个,但不是很有帮助。谁能帮我吗?

回答by paxdiablo

It says it right there in the documentation:

它在文档中说

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll.

请注意,替换字符串中的反斜杠 (\) 和美元符号 ($) 可能会导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll

And, in Matcher.replaceAll:

并且,在Matcher.replaceAll

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

请注意,替换字符串中的反斜杠 (\) 和美元符号 ($) 可能导致结果与将其视为文字替换字符串时的结果不同。美元符号可以被视为对上述捕获子序列的引用,反斜杠用于转义替换字符串中的文字字符。

What you need to do is to escape any escape characters you have in the replacement string, such as with Matcher.quoteReplacement():

您需要做的是转义替换字符串中的任何转义字符,例如Matcher.quoteReplacement()

import java.io.File;
import java.util.regex.Matcher;

class Test {
    public static void main(String[] args) {
        String s = "a/b/c/d";
        String sep = "\"; // File.separator;
        s = s.replaceAll("/", Matcher.quoteReplacement(sep));
        System.out.println(s);
    }
}

Note, I'm using the literal \\in seprather than using File.separatordirectly since my separator is the UNIX one - you should be able to just use:

请注意,我使用的是文字\\insep而不是File.separator直接使用,因为我的分隔符是 UNIX 分隔符 - 您应该可以使用:

s = s.replaceAll("/", Matcher.quoteReplacement(File.separator));

This outputs:

这输出:

a\b\c\d

as expected.

正如预期的那样。

回答by Smit

Try This

试试这个

    String str = "a/b/c/d";
    str = str.replace("/", File.separator);

回答by nullpotent

File.separatoris \on Windows, that is; it is an escaped backslash. However, in a replacement string, it means something completely different. So you'd have to escape it twice, once for Java, and once for replacement string.

File.separator\在Windows上,那就是; 它是一个转义的反斜杠。但是,在替换字符串中,它的含义完全不同。因此,您必须将它转义两次,一次用于 Java,一次用于替换字符串。

This should work:

这应该有效:

"a/b/c/d".replaceAll("/", Matcher.quoteReplacement(File.separator));

回答by fmartelli

Try with str.replace('/', File.separatorChar)

试试 str.replace('/', File.separatorChar)