首页 > 编程语言 > 详细

Java如何替换所有指定(出现)的字符串?

时间:2018-09-10 10:22:29      阅读:133      评论:0      收藏:0      [点我收藏+]

在Java编程中,如何替换所有指定(出现)的字符串?

以下示例演示如何使用Matcher类的replaceAll()方法替换字符串中的所有出现的子字符串。

package com.yiibai;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplaceAllOccurrences {
    public static void main(String args[]) {
        Pattern p = Pattern.compile("Java");
        String instring = "Java and Javascript all is the best languages.";
        System.out.println("initial String: " + instring);
        Matcher m = p.matcher(instring);
        String tmp = m.replaceAll("Python");
        System.out.println("String after replacing 1st Match: " + tmp);
    }
}
Java

上述代码示例将产生以下结果 -

initial String: Java and Javascript all is the best languages.
String after replacing 1st Match: Python and Pythonscript all is the best languages.

 

Java如何替换所有指定(出现)的字符串?

原文:https://www.cnblogs.com/borter/p/9617159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!