英文:
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)?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论