0

I have a string that contains String q = "What's the value of √32 and √83?";, my problem is replacing √num with sqrt(32) and sqrt(83).

That means my string should output : -

What's the value of sqrt(32) and sqrt(83)

is it possible?

  • Are you trying to replace it programatically? like find every sqrt symble and replace it with `sqrt()`? – Luke Oct 16 '20 at 06:27
  • 1
    Does this answer your question? [Java String.replace/replaceAll not working](https://stackoverflow.com/questions/29975776/java-string-replace-replaceall-not-working) – Michael Welch Oct 16 '20 at 22:44

1 Answers1

5

Use a regex replacement on the pattern √(\d+)\b, and replace with sqrt(...):

String q = "What's the value of √32 and √83?";
String output = q.replaceAll("√(\\d+)\\b", "sqrt($1)");
System.out.println(output);

This prints:

What's the value of sqrt(32) and sqrt(83)?
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360