And replace the difference replaceAll

Outline

Replace the parameters and CharSequence char, which can support the replacement character, and also supports the replacement string (CharSequence i.e., meaning a string sequence, i.e. a string that white)

ReplaceAll parameter is regex, which is based on regular expressions to replace, such as: can replaceAll ( "\ d", "*") to a string of all-numeric characters are replaced by an asterisk

Same point

The same point: All are replaced, or string that is a character in the source string replaced all or specified character string

difference

Different points: replaceAll support regular expressions, so the argument will be resolved (two parameters are), as replaceAll ( "\ d", " "), but does not replace, replace ( "\ d", " " ) is to replace the string "\ d", and does not resolve to a regular

There is also a difference: "\" is an escape character in java, you need a two representatives. For example System.out.println ( "\"); print out only a "." But the "\" is a regular expression escape character, needed a two representatives. So: \\ java is converted to \, \ has been converted into a regular expression \, so replace "\" to "\" will use replaceAll with replaceAll ( "\\", "\\\\"), and replace the replace ( "\", "\\")

If you want to replace the first occurrence, you can use replaceFirst (), this method is also based on regular expressions to replace, but replaceAll () is different, replace only the first occurrence of the string

发布了52 篇原创文章 · 获赞 282 · 访问量 13万+

Guess you like

Origin blog.csdn.net/qq_43733123/article/details/103814251
Recommended