在一些字符之间放置空格

时间:2014-10-02 10:27:05

标签: java string replace operators

我想在数学运算符之后和之前放置空格:

例如:

55/2 * 456-5 + 5

结果: 55/2 * 456 - 5 + 5

1 个答案:

答案 0 :(得分:0)

一种方法:

String lEquation = "55/2*456-5+5";
lEquation = lEquation.replaceAll("\\/"," \\/ ").replaceAll("\\*"," \\* ").replaceAll("-"," - ").replaceAll("\\+"," \\+ ");
System.out.println("My equation: "+ lEquation);

结果将是:

My equation: 55 / 2 * 456 - 5 + 5