博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java字符串replaceAll()方法
阅读量:2533 次
发布时间:2019-05-11

本文共 4790 字,大约阅读时间需要 15 分钟。

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);    }}
Replace Single Character 1
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);    }}
Replace String
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
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);    }}
Replace Space
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);    }}
Hiding Numbers
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
vals = new HashMap
(); vals.put("#Employee#", "John"); vals.put("#Salary#", "12000"); for (String key : vals.keySet()) { s = s.replaceAll(key, vals.get(key)); } System.out.println(s); }}
Custom Messages
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);    }}
ReplaceFirst
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 .

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

翻译自:

转载地址:http://gslzd.baihongyu.com/

你可能感兴趣的文章
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
查看>>
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>
139句
查看>>
购买《哈利波特》书籍
查看>>
谈谈一些网页游戏失败的原因到底有哪些?(转)
查看>>
备案的问题
查看>>
asp.net下ajax.ajaxMethod使用方法
查看>>
win10+mongodb安装配置
查看>>
window7修改hosts文件
查看>>
【Leetcode_easy】720. Longest Word in Dictionary
查看>>
地铁时光机第一阶段冲刺一
查看>>
Code Smell那么多,应该先改哪一个?
查看>>
站立会议02(一期)
查看>>
oracle数据库导入导出问题
查看>>