String replace()和replaceAll()之间的区别 [英] Difference between String replace() and replaceAll()

查看:934
本文介绍了String replace()和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆