2013-10-09 159 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)

相关问题