更改“\”到“/”
我正在寻找一段简单的代码,它将使用 java 将字符串中的所有反斜杠更改为正斜杠。
我试过这个: word.replaceAll("\","/");
但这是行不通的。有人能快速解决这个问题吗?
谢谢
我还注意到我的字符串操作几乎都不起作用。我尝试了像 toUpperCase() 这样的东西,但字符串没有发生任何变化?!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
replaceAll()
是错误使用的方法,因为它使用 正则表达式来匹配。您想要更简单的 替换文字的
replace()
方法。试试这个:注意:
"\\"
是编码单个反斜杠字符串的方式 StringmyString = myString.someMethod();
replaceAll()
is the wrong method to use in this case, because it uses regular expressions to match.You want the simpler
replace()
method that replaces literals. Try this:Notes:
"\\"
is how you code a String that is a single backslashmyString = myString.someMethod();
这些函数将返回一个新字符串,其中包含您尝试实现的更改,而不更改字符串本身。在
\
情况下,您可能需要使用转义斜杠 (\\
)the functions will return a new string with the changes you are trying to implement not change the string itself. in the
\
case you may need to use an escaped slash (\\
)在这种情况下,您需要使用:
toUpperCase()
返回一个字符串,它不会更改调用该方法的字符串。http://docs.oracle.com/javase /1.5.0/docs/api/java/lang/String.html
In this case you would need to use:
toUpperCase()
returns a string, it does not change the string calling the method.http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
字符串不可变。
他们永远无法改变。
您需要编写
word = word.something()
。Strings are immutable.
They can never change.
You need to write
word = word.something()
.你需要做的就是 word = word.replaceAll("\", "/");
您必须知道字符串无法更改。
这些方法的工作原理是迭代字符串的字符并用正斜杠替换所有反斜杠
all you need to do is word = word.replaceAll("\", "/");
you have to know that a string can't be changed.
those methods work by iterating over the characters of the string and replacing all the backlashes with forward slash