Java String replaceAll() replaces all the occurrences of a particular character, string or a regular expression in the string. The method returns a new string as its output. The method requires two parameters.

Java字符串replaceAll()替换所有出现的特定字符,字符串或字符串中的正则表达式。 该方法返回一个新字符串作为其输出。 该方法需要两个参数。

字符串replaceAll()语法 (String replaceAll() Syntax)


public String replaceAll(String regex, String replacement)

A regex that represents the pattern to look for while replacing and a replacement string to replace it with.

一个正则表达式 ,代表替换时要查找的模式以及替换 字符串

There are multiple types of substitutions that are possible with String replaceAll() method.

字符串replaceAll()方法可能有多种替换类型。

替换单个字符 (Replacing a single character )

To replace a single character, specify the character to be replaced as the regex.

要替换单个字符,请指定要替换的字符作为正则表达式。


public class Main {public static void main(String[] args) {String s = "Welcome to JournalDev";System.out.println("Original String : "+s);System.out.println();String rep = s.replaceAll("o","@");System.out.println("New String: "+rep);}
}

Original String: Welcome to JournalDevNew String: Welc@me t@ J@urnalDev

替换字符序列 (Replace sequence of characters )

To replace a sequence of characters, mention the sequence as the regex in the replaceAll() function.

要替换字符序列,请在replaceAll()函数中将该序列称为正则表达式。


public class Main {public static void main(String[] args) {String s = "Welcome to JournalDev";System.out.println("Original String : "+s);System.out.println();String rep = s.replaceAll("to","this is");System.out.println("New String: "+rep);}
}
Output
输出量

Original String : Welcome to JournalDevNew String: Welcome this is JournalDev

删除/替换空间 (Remove/Replace Spaces)

The replaceAll() method can remove or replace the whitespaces in your text string.

replaceAll()方法可以删除或替换文本字符串中的空格。


public class Main {public static void main(String[] args) {String s = "Welcome to JournalDev";System.out.println("Original String : "+s);System.out.println();String rep = s.replaceAll(" ","");System.out.println("New String: "+rep);}
}
Remove Spaces
删除空间

Original String : Welcome to JournalDevNew String: WelcometoJournalDev

Similarly, to replace the whitespaces:

同样,要替换空白:


public class Main {public static void main(String[] args) {String s = "Welcome to JournalDev";System.out.println("Original String : "+s);System.out.println();String rep = s.replaceAll(" ","_");System.out.println("New String: "+rep);}
}

Original String : Welcome to JournalDevNew String: Welcome_to_JournalDev

隐藏文本中的数字 (Hiding numeric digits in a text )

The replaceAll() can hide numeric digits that appear in a string. The digits can be replaced by ‘*’.

replaceAll()可以隐藏出现在字符串中的数字。 这些数字可以用“ *”代替


public class Main {public static void main(String[] args) {String s = "Hello! this is the number : 1234 ";System.out.println("Original String : "+s);System.out.println();String rep = s.replaceAll("[0-9]","*");System.out.println("New String: "+rep);}
}

Original String : Hello! this is the number : 1234 New String: Hello! this is the number : ****

创建自定义消息 (Creating custom messages )

replaceAll() can replace the strings in a generic string to generate a custom message. Let’s take values from a map and generate a custom message.

replaceAll()可以替换通用字符串中的字符串以生成自定义消息。 让我们从地图中获取值并生成自定义消息。


import java.util.HashMap;
import java.util.Map;public class Main {public static void main(String[] args) {String s = "dear #Employee#, your Current Salary is #Salary#.";Map<String, String> vals = new HashMap<String, String>();vals.put("#Employee#", "John");vals.put("#Salary#", "12000");for (String key : vals.keySet()) {s = s.replaceAll(key, vals.get(key));}System.out.println(s);}
}

dear John, your Current Salary is 12000.

首先替换 (Replace First )

Unlike replceAll(), replaceFirst() only replaces the first instance of the pattern.

与replceAll()不同,replaceFirst()仅替换模式的第一个实例。


public class Main {public static void main(String[] args) {String s = "Welcome to JournalDev";System.out.println("Original String : "+s);System.out.println();String rep = s.replaceFirst("o","@");System.out.println("New String: "+rep);}
}

Original String: Welcome to JournalDevNew String: Welc@me to JournalDev

结论 (Conclusion)

The replaceAll() method replaces a regex with a string. This is different from the simple replace() method that only performs character substitutions. You can learn more about the repalceAll() function from its official documentation.

replaceAll()方法将正则表达式替换为字符串。 这与仅执行字符替换的简单replace()方法不同。 您可以从repalceAll()函数的正式文档中了解更多信息。

翻译自: https://www.journaldev.com/41936/java-string-replaceall

Java字符串replaceAll()方法相关推荐

  1. 使用java中replaceAll方法替换字符串中的反斜杠

    今天在项目中使用java中replaceAll方法将字符串中的反斜杠("\")替换成空字符串(""),结果出现如下的异常: 1 java.util.regex. ...

  2. java字符串反转方法【全】

    搜集的java字符串反转方法,一般用于面试,项目中用的很少··· package com.wsheng.aggregator.algorithm.string;import java.util.Sta ...

  3. java字符串的方法 1118

    java字符串的方法 字符串的定义 方式一 String 变量名 = "内容"; 方式二 String 变量名 = new String(); 方式三 String 变量名 = n ...

  4. java字符串分割方法.split()的详细用法

    先看看它在java包中的Java API是: java.lang.String 方法总结 (1)按指定普通字符分割: java代码如下:String string="123@456@789& ...

  5. Java字符串截取 方法

    在 String 中提供了两个截取字符串的方法,一个是从指定位置截取到字符串结尾,另一个是截取指定范围的内容. 方法的重载: public String substring(int beginInde ...

  6. [转载] Java字符串分割方法

    参考链接: Java中的StringTokenizer方法的示例 2 [size=medium]1.用split()方法进行分割,分割开的子字符串放入数组,然后进行处理. 示例代码如下: public ...

  7. Java字符串分割方法split()的功能以及使用方法的详细介绍!

    在Java的学习中经常会遇到按照自己的理想情况分割字符串,并根据分割后的结果进行后续操作的问题.对于这些问题就可以使用Java提供的split() 方法进而实现,下面详细的介绍它的功能以及使用方法. ...

  8. java 字符串 startswith_startsWith方法——判断前缀字符串

    startsWith方法测试此字符串从指定索引开始的子字符串是否以指定前缀开始. 语法1  public boolean startsWith(String prefix , int toffset) ...

  9. 我的Android进阶之旅------Java字符串格式化方法String.format()格式化float型时小数点变成逗号问题...

    今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.好吧,又是我来维护. 好吧,先把系统语言切换到波兰语,切换到波兰语的方法查看文章 我的And ...

最新文章

  1. SAP RETAIL 商品主数据里的X-DChain Status字段
  2. 山西五台警方通报“男子强拽女学生”:嫌疑人被刑拘
  3. python中表示空类型的是_python中怎么表示空值
  4. git 常用命令思维导图
  5. PowerShell 远程执行任务
  6. TypeScript 2019 路线图:更效率,更易用!
  7. java形状函数_java基础:10.4 Java FX之形状
  8. vmd与ovito的对比
  9. 全链路异步Rest客户端 ESA RestClient
  10. 高颜值智能存储 华三魔术家M2无线云盘评测
  11. xp系统从u盘启动计算机,一键u盘装xp系统,教您如何使用U盘装xp系统
  12. SpringBoot——检索
  13. 云计算安全测评:云应用安全
  14. python爬取笔趣阁
  15. Android 下拉选择框自定义view
  16. 视频教程-2019 react入门至高阶实战,含react hooks-ReactJS
  17. Spring Boot 2.0 配置图文教程 1
  18. dolphinscheduler搭建以及搭建使用中遇到的问题
  19. python 游程编码_游程编码(字符串中字母的查找/打印频率)
  20. “前程无忧”招聘数据爬虫——(1)

热门文章

  1. Google及其云智慧
  2. [转载] Python语言程序设计基础(第二版)嵩天等课后习题答案
  3. [转载] python中的Numpy库入门
  4. [转载] [Python错误]NameError: name ‘name’ is not defined
  5. [转载] 如何使用 Python 生成酷炫的二维码?
  6. [转载] java简易爬虫Crawler
  7. 利用apache 的PropertyUtilsBean 实现map和pojo相互转换
  8. 字符转获取拼音首字母php实现
  9. js datagrid 移动去重
  10. Codeforces Round #310 (Div. 1) B. Case of Fugitive set