java matcher replace_Java Matcher replaceAll(String)用法及代码示例

Matcher类的replaceAll(String)方法的行为类似于append-and-replace方法。此方法读取输入字符串,并将其替换为匹配字符串中的匹配模式。

用法:

public String replaceAll(String stringToBeReplaced)

参数:此方法采用参数stringToBeReplaced,它是匹配器中要替换的String。

返回值:此方法返回一个String,其目标String通过替换String进行构造。

下面的示例说明Matcher.replaceAll()方法:

示例1:

// Java code to illustrate replaceAll() method

import java.util.regex.*;

public class GFG {

public static void main(String[] args)

{

// Get the regex to be checked

String regex = "(Geeks)";

// Create a pattern from regex

Pattern pattern = Pattern.compile(regex);

// Get the String to be matched

String stringToBeMatched

= "GeeksForGeeks Geeks for For Geeks Geek";

// Create a matcher for the input String

Matcher matcher

= pattern.matcher(stringToBeMatched);

System.out.println("Before Replacement: "

+ stringToBeMatched);

// Get the String to be replaced

String stringToBeReplaced = "GFG";

StringBuilder builder

= new StringBuilder();

// Replace every matched pattern

// with the target String

// using replaceAll() method

System.out.println("After Replacement: "

+ matcher

.replaceAll(stringToBeReplaced));

}

}

输出:

Before Replacement: GeeksForGeeks Geeks for For Geeks Geek

After Replacement: GFGForGFG GFG for For GFG Geek

示例2:

// Java code to illustrate replaceAll() method

import java.util.regex.*;

public class GFG {

public static void main(String[] args)

{

// Get the regex to be checked

String regex = "(FGF)";

// Create a pattern from regex

Pattern pattern = Pattern.compile(regex);

// Get the String to be matched

String stringToBeMatched

= "GFGFGFGFGFGFGFGFGFG FGF GFG GFG FGF";

// Create a matcher for the input String

Matcher matcher

= pattern.matcher(stringToBeMatched);

// Get the String to be replaced

String stringToBeReplaced = "GFG";

StringBuilder builder

= new StringBuilder();

// Replace every matched pattern

// with the target String

// using replaceAll() method

System.out.println("After Replacement: "

+ matcher

.replaceAll(stringToBeReplaced));

}

}

输出:

After Replacement: GGFGGGFGGGFGGGFGGFG GFG GFG GFG GFG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
通过源码分析,我们可以得出以下结论: 1. `String.replace()` 和 `String.replaceAll()` 调用的方法是一样的,都是 `Matcher.replaceAll()` 方法。 2. `replaceAll()` 方法没有传入参数 `Pattern.LITERAL`。 根据源码的定义,我们可以进一步了解这两个方法的区别: 1. `replace(CharSequence target, CharSequence replacement)` 方法是用指定的文字替换序列替换与文字目标序列匹配的字符串的每个子字符串。 2. `replaceAll(String regex, String replacement)` 方法是将与给定正则表达式匹配的字符串的每个子字符串替换为给定的替换。 综上所述,我们可以总结如下: 1. `replace()` 和 `replaceAll()` 都是用于替换字符串的常用方法。 2. 两者都是全部替换,可以将源字符串中的某一字符或字符串全部替换为指定的字符或字符串。 3. 如果只想替换第一次出现的字符串,可以使用 `replaceFirst()` 方法。 4. `replaceAll()` 和 `replaceFirst()` 方法都基于规则表达式进行替换,但不同之处在于,`replaceFirst()` 只替换第一次出现的字符串。 5. `replaceAll()` 和 `replaceFirst()` 方法所使用的替换参数可以是普通字符串,也可以是正则表达式。 6. 如果 `replaceAll()` 和 `replaceFirst()` 方法所使用的参数不是基于规则表达式的,那么它们的效果和效率与 `replace()` 替换字符串的方法相同。 所以,`replaceAll()` 和 `replace()` 方法的主要区别在于是否基于正则表达式进行匹配和替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值