如何替换字符串的一部分。特别\是/在Java中。我尝试使用string.replace("". ""),但这不起作用。
2 回答
1
这已按时得到数百万的答复:
myString.replace("\\", "/")
可能你的困惑来自你必须逃避它的事实。
于 2013-10-09T19:24:47.107 回答
1
如果要替换字符,可以使用
replace(char toReplace, char replacement)likeyourString = 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)
于 2013-10-09T19:41:05.263 回答