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

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

问题描述

目前我正在做

示例:

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

我觉得它很难看。不确定更好的方法吗?也许循环遍历一个hashmap?

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 Lang StringUtils class。

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

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

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

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