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
,replace
方法接受一对字符或一对CharSequence
's(其中 String 是一个子类,所以它会很乐意接受一对 String 的).replace
方法将替换所有出现的字符或 CharSequence
.另一方面,replaceFirst
和 replaceAll
的第一个 String
参数是正则表达式(regex).使用错误的函数可能会导致细微的错误.
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, the first String
arguments of replaceFirst
and replaceAll
are regular expressions (regex). Using the wrong function can lead to subtle bugs.
这篇关于String replace() 和 replaceAll() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!