Here you can find the source of replace(String str, String replacement, Function
Parameter | Description |
---|---|
str | a parameter |
replacement | a parameter |
func | a parameter |
public static String replace(String str, String replacement, Function<Matcher, String> func)
//package com.java2s; //License from project: Open Source License import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**//from ww w .j a v a 2s . c om * * @param str * @param replacement * @param func * @return */ public static String replace(String str, String replacement, Function<Matcher, String> func) { return replace(str, replacement, func, 0); } /** * * @param str * @param replacement * @param func * @param flags * @return */ public static String replace(String str, String replacement, Function<Matcher, String> func, int flags) { if (str == null || str.isEmpty()) return str; Pattern p = Pattern.compile(replacement, flags); Matcher m = p.matcher(str); boolean result = m.find(); StringBuffer sb = new StringBuffer(); do { String res = func.apply(m); m.appendReplacement(sb, res); result = m.find(); } while (result); m.appendTail(sb); return sb.toString(); } }