替换字符串中 的 子字符串

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RegexMatches {

private static String REGEX = "dog";

private static String INPUT = "The dog says meow. " + "All dogs say meow.";

private static String REPLACE = "cat";

public static void main(String[] args) {

Pattern p = Pattern.compile(REGEX);

// get  a  matcher  object

Matcher m = p.matcher(INPUT);

INPUT = m.replaceAll(REPLACE);

System.out.println(INPUT); }

}

 

=== 

//          StringBuffer 的方法   replace(int start, int end, String str)
posted @ 2020-09-08 22:20  winner66  阅读(165)  评论(0)    收藏  举报