String replace()和replaceAll()之间的区别 [英] Difference between String replace() and replaceAll()
问题描述
java.lang.String的 replace()
和 replaceAll()
方法之间有什么区别,
除了以后使用正则表达式?对于简单的替换,例如,用 /
替换。
,
有什么区别吗?
What's the difference between java.lang.String 's replace()
and replaceAll()
methods,
other than later uses regex? For simple substitutions like, replace .
with /
,
is there any difference?
推荐答案
在 java.lang.String
,替换
方法需要一个一对char或一对 CharSequence
(其中String是一个子类,因此它很乐意接受一对String)。 替换
方法将替换所有出现的char或 CharSequence
。另一方面, String
参数 replaceFirst
和 replaceAll
是正则表达式(正则表达式)。使用错误的功能可能会导致细微的错误。
In java.lang.String
, the replace
method either takes a pair of char's or a pair of CharSequence
's (of which String is a subclass, so it'll happily take a pair of String's). The replace
method will replace all occurrences of a char or CharSequence
. On the other hand, both String
arguments to replaceFirst
and replaceAll
are regular expressions (regex). Using the wrong function can lead to subtle bugs.
这篇关于String replace()和replaceAll()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!