用Java替换字符串中的多个字符 [英] Replace multiple characters in a string in Java

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

问题描述

我有一些方程式字符串,格式如下((a + b)/(c +(d * e)))

I have some strings with equations in the following format ((a+b)/(c+(d*e))).

我还有一个文本文件,其中包含每个变量的名称,例如:

I also have a text file that contains the names of each variable, e.g.:

a velocity
b distance
c time

等...

对我来说编写代码的最佳方式是什么,以便插入 velocity 无处不在 a 发生,距离 b ,依此类推?

What would be the best way for me to write code so that it plugs in velocity everywhere a occurs, and distance for b, and so on?

推荐答案

对于字符串 str ,请使用 replaceAll()功能:

For string str, use the replaceAll() function:

str = str.toUpperCase();  //Prevent substitutions of characters in the middle of a word
str = str.replaceAll("A", "velocity");
str = str.replaceAll("B", "distance");
//etc.

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

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