2013-10-09 157 views

回答

1

這已經回答了上百萬時間:

myString.replace("\\", "/") 

也許你的困惑來自於一個事實,你要逃避它。

+0

你需要轉義反斜槓,因爲這是一個字符串。 –

+0

謝謝,我在寫完之後才意識到 –

+0

@AntonioMG - 我的印象應該是'myString.replace(「\\\\」,「/」)'?我從來沒有看到使用'「\\」'編譯錯誤,但是當我使用'「\\」'時,我看到了運行時錯誤 - 有人可以解釋什麼時候只能使用2 vs當你必須使用4時? –

1
  • 如果要替換的字符,你可以用replace(char toReplace, char replacement)

    yourString = yourString.replace('\\', '/');// since \ is special character in Java 
    //to create its literal you need to write it with another \ before '\\' 
    
  • 如果您需要更換子做使用replace(String yourSubstring, String replacement)(注意,這將使用正則表達式的機制,但會逃跑的正則表達式元字符所以替換單個字符replace(char1, char2)更快)。

  • 如果你想更換是不一樣的几子,但可以用正則表達式來描述,您可以使用replaceAll(regex, replacement)replaceFirst(regex, replacement)

相關問題