使用替换字符串中多个单词的最有效方法 [英] Most efficient way to use replace multiple words in a string

查看:13
本文介绍了使用替换字符串中多个单词的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在做

示例:

line.replaceAll(",","").replaceAll("cat","dog").replaceAll("football","rugby");

我认为它丑陋.不确定有更好的方法来做到这一点吗?也许循环遍历哈希图?

I think that it ugly. Not sure a better way to do this? Maybe loop through a hashmap?

效率是指更好的代码风格和灵活性

By efficiency I mean better code style and flexibility

推荐答案

此功能已在 Commons LangStringUtils 类.

This functionality is already implemented in Commons Lang's StringUtils class.

StringUtils.replaceEach(String text, String[] searchList, String[] replacementList)

这篇关于使用替换字符串中多个单词的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆