替换字符串中的所有 √num,Java

huangapple go评论70阅读模式
英文:

Replace all √num from a string java

问题

我有一个包含 String q = "What's the value of √32 and √83?"; 的字符串,我的问题是将其中的 √num 替换为 sqrt(32)sqrt(83)

这意味着我的字符串应该输出:-

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

这可能吗?

英文:

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?

答案1

得分: 5

使用正则表达式替换模式√(\d+)\b,并替换为sqrt(...)

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

这将打印出:

What's the value of sqrt(32) and sqrt(83)?
英文:

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)?

huangapple
  • 本文由 发表于 2020年10月16日 14:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定