在 Java 中,String 类提供了 3 种字符串替换方法,分别是 replace()、replaceFirst() 和 replaceAll(),本文将详细介绍它们的使用方法。

replace() 方法

replace() 方法用于将目标字符串中的指定字符(串)替换成新的字符(串),其语法格式如下:

字符串.replace(String oldChar, String newChar)

其中,oldChar 表示被替换的字符串;newChar 表示用于替换的字符串。replace() 方法会将字符串中所有 oldChar 替换成 newChar。

例 1

创建一个字符串,对它使用 replace() 方法进行字符串替换并输出结果。代码如下:

public static void main(String[] args) {
String words = "hello java,hello php";
System.out.println("原始字符串是'"+words+"'");
System.out.println("replace(\"l\",\"D\")结果:"+words.replace("l","D"));
System.out.println("replace(\"hello\",\"你好\")结果:"+words.replace("hello","你好 "));
words = "hr's dog";
System.out.println("原始字符串是'"+words+"'");
System.out.println("replace(\"r's\",\"is\")结果:"+words.replace("r's","is"));
}

输出结果如下所示:

原始字符串是'hello java,hello php'
replace("l","D")结果:heDDo java,heDDo php
replace("hello","你好")结果:你好 java,你好 php
原始字符串是'hr's dog'
replace("r's","is")结果:his dog

replaceFirst() 方法

replaceFirst() 方法用于将目标字符串中匹配某正则表达式的第一个子字符串替换成新的字符串,其语法形式如下:

字符串.replaceFirst(String regex, String replacement)

其中,regex 表示正则表达式;replacement 表示用于替换的字符串。例如:

String words = "hello java,hello php";
String newStr = words.replaceFirst("hello","你好 ");
System.out.println(newStr); // 输出:你好 java,hello php

eplaceAll() 方法

replaceAll() 方法用于将目标字符串中匹配某正则表达式的所有子字符串替换成新的字符串,其语法形式如下:

字符串.replaceAll(String regex, String replacement)

其中,regex 表示正则表达式,replacement 表示用于替换的字符串。例如:

String words = "hello java,hello php";
String newStr = words.replaceAll("hello","你好 ");
System.out.println(newStr); // 输出:你好 java,你好 php

Java字符串的替换(replace()、replaceFirst()和replaceAll())的更多相关文章

  1. java字符串的替换replace、replaceAll、replaceFirst的区别详解

    如果不是刚刚复习了下正则表达式,我可能也不会注意到,原来String的replaceAll跟replaceFirst用到了正则表达式! 不多解释,看代码: String s = "my.te ...

  2. java字符串的替换replace、replaceAll、replaceFirst的区别

    看代码: String s = "my.test.txt"; System.out.println(s.replace(".", "#")) ...

  3. JAVA字符串的替换replace、replaceAll、replaceFirst的区别解析。

    String str = "i.like.cat"; System.out.println(str.replace(".", "!")); ...

  4. java字符串的替换

    split也是用到了正则表达式 replace 是没有用正则表达式,全部替换 replaceAll 和replaceFirst是用了正则表达式 replaceAll替换所有,replaceFirst是 ...

  5. 字符串正则替换replace第二个参数是函数的问题

    按照JS高程的说法,如下 replace()方法的第二个参数也可以是一个函数.在只有一个匹配项(即与模式匹配的字符串)的情况下,会向这个函数传递3个参数:模式的匹配项.模式匹配项在字符串中的位置和原始 ...

  6. java多次替换(replace不行)

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class test { public static vo ...

  7. 字符串正则替换replace第二个参数是函数

    zepto中 //将字符串转成驼峰式的格式 camelize = function (str) { return str.replace(/-+(.)?/g, function (match, chr ...

  8. js |竖线字符串全部替换 replace

    一般人解决方案: 'a|b|c'.replace(/\|/g, ','); 神经病解决方案: 'a|b|c'.split('|').join(',');

  9. Python replace() 和 re.sub() 字符串字符替换

    Python replace() 和 re.sub() 字符串字符替换 replace() testStr = 'aa:bb[cc' testStr.replace(':','_') 每次只能替换一个 ...

随机推荐

  1. DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)

    DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★★(5星) 这个时间系列想 ...

  2. 使用zookeeper报错 stat is not executed because it is not in the whitelist. envi is not executed because it is not in the whitelist.

    在使用四字命令或者zk ui界面查看zookeeper集群时,出现如下提示: stat is not executed because it is not in the whitelist. envi ...

  3. node工具之nodemon

    nodemon nodemon是一种工具,可以自动检测到目录中的文件更改时通过重新启动应用程序来调试基于node.js的应用程序. 安装 npm install -g nodemon //或 npm ...

  4. [转载]C++二维动态数组memset()函数初始化

    来源:https://blog.csdn.net/longhopefor/article/details/20994919 先说说memset函数: void *memset(void *s,int ...

  5. Scala新版本学习(1):

    1.进官网:https://www.scala-lang.org/ 上面就是进入Scala社区后的一个画面,官方对Scala的简单介绍是:Scala将面向对象和函数式编程集合在一个简洁的高级语言中,S ...

  6. js的作用主要这么几个

    js的作用主要有这么几个表单验证:网页上,用户输入的信息需要进行验证,在客户端验证,可以减少对服务器端的压力.所以,你应该把握正则表达式方面的知识.网页特效:页面上很多特效是非常好的,能产生很好的用户 ...

  7. Vue自定义组件以及组件通信的几种方式

    本帖子来源:小贤笔记 功能 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它 ...

  8. python视频学习笔记5(高级变量的类型)

    知识点回顾: Python 中数据类型可以分为 **数字型** 和 **非数字型*** 数字型 * 整型 (`int`) * 浮点型(`float`) * 布尔型(`bool`) * 真 `True` ...

  9. 自动化测试报告之allure使用基础指南

    差不多三个月前些的教程,然后跳槽了,自定义模块还没有写....后续也不知道有时间补上没有,最近应该会毕竟专注app测试这块了     1.github下载allure安装包:https://githu ...

  10. CentOS7使用阿里云的yum源

    替换成阿里云的yum源速度更快一些,替换很简单,简单记录一下步骤 1.备份原来的yum源 sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repo ...