繁体   English   中英

当我尝试将`\\\\`替换为`\\`时,为什么会出现StringIndexOutOfBoundsException?

[英]Why am I getting a StringIndexOutOfBoundsException when I try to replace `\\` with `\`?

我必须在Java中用\\替换\\\\ 我正在使用的代码是

System.out.println( (MyConstants.LOCATION_PATH + File.separator + myObject.getStLocation() ).replaceAll("\\\\", "\\") );

但我不知道它为什么抛出StringIndexOutOfBoundsException

它表示String index out of range: 1

可能是什么原因? 我想这是因为第一个参数replaceAll接受一个模式。 可能的解决方案是什么?


堆栈跟踪

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:558)
    at java.util.regex.Matcher.appendReplacement(Matcher.java:696)
    at java.util.regex.Matcher.replaceAll(Matcher.java:806)
    at java.lang.String.replaceAll(String.java:2000)

找到答案

asalamon74发布了我需要的代码,但我不知道为什么他删除了它。 无论如何这是它。

Java的bug数据库中已经存在一个bug (感谢您的参考,asalamon。)

yourString.replaceAll("\\\\", "\\\\");

令人惊讶的是,搜索和替换字符串都是相同的:)但它仍然做我需要的。

使用String.replace而不是replaceAll来避免使用正则表达式:

String original = MyConstants.LOCATION_PATH + File.seperator 
    + myObject.getStLocation();
System.out.println(original.replace("\\\\", "\\"));

就个人而言,我不会这样做 - 我将MyConstants.LOCATION_PATH_FILE创建为File然后你可以写:

File location = new File(MyConstants.LOCATION_PATH_FILE,
                         myObject.getStLocation());

这将自动做正确的事情。

好吧,我试过了

    String test = "just a \\ test with some \\\\ and others \\\\ or \\ so";
    String result = test.replaceAll("\\\\", "\\\\");
    System.out.println(test);
    System.out.println(result);
    System.out.println(test.equals(result));

并且像预期的那样得到了

just a \ test with some \\ and others \\ or \ so
just a \ test with some \\ and others \\ or \ so
true

你真正需要的

string.replaceAll("\\\\\\\\", "\\\\");

要得到

just a \ test with some \\ and others \\ or \ so
just a \ test with some \ and others \ or \ so
false

你想找到: \\\\ (2斜杠)
需要在正则表达式中转义: \\\\\\\\ (4斜杠)
并在Java中逃脱: "\\\\\\\\\\\\\\\\" (8斜杠)
同样替换......

对于正则表达式,如果要将\\更改为\\\\ ,则应执行以下操作:

if (str.indexOf('\\') > -1)
    str = str.replaceAll("\\\\", "\\\\\\\\");
str = "\"" + str + "\"";

其中\\\\\\\\表示\\\\\\\\\\\\\\\\\\表示\\\\

File.seperator已经像任何字符串对象一样进行了转义,因此您将两次转义它们。

您只需将要输入的值转义为字符串文字。

试试这个

cadena.replaceAll("\\\\","\\\\\\\\")

最好的方法是:

str.replace(**'**\\**'**, **'**/**'**); //with char method not String

我怀疑问题是replaceAll()使用regexps并且反斜杠是regexp和Java中的转义字符 - 可能需要加倍反斜杠的数量。

通常,您应该始终发布完整的异常堆栈跟踪,以这种方式诊断问题要容易得多。

我相信你需要做的是:

System.out.println( (MyConstants.LOCATION_PATH + File.separator + myObject.getStLocation() ).replaceAll("\\\\\\\\", "\\\\") );

正则表达式String实际上是四个反斜杠,这是一个匹配两个反斜杠的正则表达式。

根据Java文档,替换String必须是四个斜杠,来自: http//java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String) )

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

final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(str);
char character =  iterator.current();
while (character != CharacterIterator.DONE )
{
  if (character == '\\\\') {
     result.append("\\");
  }
  else {
    result.append(character);
  }

  character = iterator.next();
}

System.out.print(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM