开发者社区> 问答> 正文

Java 中如何用 replaceAll() 替换指定文字?

Java 中如何用 replaceAll() 替换指定文字?

展开
收起
vncamyi27xznk 2021-11-12 19:30:05 496 0
1 条回答
写回答
取消 提交回答
  • "//替换指定{}中文字 String str = ""Java目前的发展史是由{0}年-{1}年""; String[][] object = { new String[] { ""\{0\}"", ""1995"" }, new String[] { ""\{1\}"", ""2007"" } }; System.out.println(replace(str, object)); public static String replace(final String sourceString, Object[] object) { String temp = sourceString; for (int i = 0; i < object.length; i++) { String[] result = (String[]) object[i]; Pattern pattern = Pattern.compile(result[0]); Matcher matcher = pattern.matcher(temp); temp = matcher.replaceAll(result[1]); } return temp; }

    这本电子书收录于开发者藏经阁,下载链接:https://developer.aliyun.com/topic/download?id=923"

    2021-11-12 22:01:53
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载