1

I have string "2x+3" and I want change 'x' to string "8".

String operation = "2x+3";
String x = "8";

And I want result such as "2*8+3"

or

String operation = "x+3";
String x = "8";

And I want result such as "8+3"

Does anyone know how to solve ?

midryuk
  • 85
  • 2
  • 8

2 Answers2

1
operation = operation.replace("x","8")
Zielu
  • 8,312
  • 4
  • 28
  • 41
0

Well you can't change the same string, because strings are immutable objects in java. What you can do is use the replace function in string class and get a new string out of it.

Have a look at this question : replace String with another in java

Community
  • 1
  • 1
Prasad Shinde
  • 652
  • 2
  • 8
  • 21