字符串替换反斜杠
如何进行反斜杠的字符串替换。
输入源字符串:
sSource = "http://www.example.com\/value";
在上面的字符串中,我想用“/”替换“\/”;
替换后的预期输出:
sSource = "http://www.example.com/value";
我从第三方获取源字符串,因此我可以控制字符串的格式。
这是我尝试过的
试验1:
sSource.replaceAll("\\", "/");
例外 索引 1 附近出现意外内部错误 \
试用2:
sSource.replaceAll("\\/", "/");
无异常,但没有进行所需的替换。不做任何事情。
试验 3:
sVideoURL.replace("\\", "/");
无异常,但未进行所需的替换。不做任何事情。
How can I do a string replace of a back slash.
Input Source String:
sSource = "http://www.example.com\/value";
In the above String I want to replace "\/" with a "/";
Expected ouput after replace:
sSource = "http://www.example.com/value";
I get the Source String from a third party, therefore I have control over the format of the String.
This is what I have tried
Trial 1:
sSource.replaceAll("\\", "/");
Exception
Unexpected internal error near index 1
\
Trial 2:
sSource.replaceAll("\\/", "/");
No Exception, but does not do the required replace. Does not do anything.
Trial 3:
sVideoURL.replace("\\", "/");
No Exception, but does not do the required replace. Does not do anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
String
是不可变的 - 您对其调用的每个方法都不会更改其状态。它返回一个保存新状态的新实例。因此,您必须将新值分配给变量(可以是同一变量)replaceAll(..)
使用正则表达式。你不需要那个。String
is immutable - each method you invoke on it does not change its state. It returns a new instance holding the new state instead. So you have to assign the new value to a variable (it can be the same variable)replaceAll(..)
uses regex. You don't need that.尝试
replaceAll("\\\\", "")
或replaceAll("\\\\/", "/")
。这里的问题是反斜杠是 (1) Java 字符串文字中的转义字符,以及 (2) 正则表达式中的转义字符 - 每个使用都需要加倍该字符,实际上需要 4
\
在行中。当然,正如 Bozho 所说,您需要对结果做一些事情(将其分配给某个变量)而不是丢弃它。在这种情况下,非正则表达式变体更好。
Try
replaceAll("\\\\", "")
orreplaceAll("\\\\/", "/")
.The problem here is that a backslash is (1) an escape chararacter in Java string literals, and (2) an escape character in regular expressions – each of this uses need doubling the character, in effect needing 4
\
in row.Of course, as Bozho said, you need to do something with the result (assign it to some variable) and not throw it away. And in this case the non-regex variant is better.
尝试
编辑:好的,即使在 stackoverflow 中也有反斜杠转义...您的 ReplaceAll 第一个字符串参数中需要有四个反斜杠...
这样做的原因是因为反斜杠被视为特殊字符的转义字符(例如 \n )实例)。
此外,replaceAll 第一个 arg 是一个正则表达式,也使用反斜杠作为转义序列。
因此,对于正则表达式,您需要传递 2 个反斜杠。要将这两个反斜杠通过java字符串传递给replaceAll,您还需要转义这两个反斜杠。
这促使你在表达式中使用四个反斜杠!这就是 java 中正则表达式的美妙之处;)
Try
Edit : Ok even in stackoverflow there is backslash escape... You need to have four backslashes in your replaceAll first String argument...
The reason of this is because backslash is considered as an escape character for special characters (like \n for instance).
Moreover replaceAll first arg is a regular expression that also use backslash as escape sequence.
So for the regular expression you need to pass 2 backslash. To pass those two backslashes by a java String to the replaceAll, you also need to escape both backslashes.
That drives you to have four backslashes for your expression! That's the beauty of regex in java ;)
您需要在源代码中屏蔽反斜杠,对于正则表达式,您需要再次屏蔽它,因此对于每个反斜杠,您需要两个反斜杠,以 4 结尾。
但
在源代码中也需要两个反斜杠。
You need to mask a backslash in your source, and for regex, you need to mask it again, so for every backslash you need two, which ends in 4.
But
needs two backslashes in source as well.
这将用字符串中的正斜杠替换反斜杠:
This will replace backslashes with forward slashes in the string:
你必须这样做
,因为对于正则表达式中源一中的字符串,反斜杠应该转义两次
you have to do
because the backshlash should be escaped twice one for string in source one in regular expression
要在特定位置替换反斜杠:
To Replace backslash at particular location: