


Introduction to the difference between replace and replaceAll in Java (code example)
本篇文章给大家带来的内容是关于Java中replace与replaceAll之间的区别介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1.java中replace API:
replace(char oldChar, char newChar):寓意为:返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
replace(CharSequence target, CharSequence replacement):寓意为:使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
replaceAll(String regex, String replacement):寓意为:使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
可以看出replace的参数是char与CharSequence,而replaceAll参数为regex(正则表达式)与replacement
2.举个栗子:
@Test public void testString(){ String str="wel2come3Souhe0"; System.out.println(str.replace("e","E")); System.out.println(str.replace('e','E')); System.out.println(str.replaceAll("\\d","A")); System.out.println(str.replaceAll("3","9")); }
执行结果为:
wEl2comE3SouhE0 wEl2comE3SouhE0 welAcomeASouheA wel2come9Souhe0
3.总结结果:replace替换字符与字符串都是一样的,replace可以根据除了字符串替换外还可以正则表达式来进行替换;
4.多了解一个:
replaceFirst(String regex, String replacement) 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
举个栗子:
@Test public void testString(){ String str="wel2come3Souhe0"; System.out.println(str.replaceFirst("\\d","A")); }
执行结果为:
welAcome3Souhe0
总结:只替换第一次出现的匹配的正则表达式;
完毕!
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
The above is the detailed content of Introduction to the difference between replace and replaceAll in Java (code example). For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools