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

查看:37
本文介绍了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.Stringreplace 方法接受一对字符或一对CharSequence's(其中 String 是一个子类,所以它会很乐意接受一对 String 的).replace 方法将替换所有出现的字符或 CharSequence.另一方面,replaceFirstreplaceAll 的第一个 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屋!

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