簡體   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