Java,如何替换字符串中的数字序列 [英] Java, how to replace a sequence of numbers in a string

查看:317
本文介绍了Java,如何替换字符串中的数字序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用括号内的数字本身替换字符串中的任何数字序列。
所以输入:

I am trying to replace any sequence of numbers in a string with the number itself within brackets. So the input:

"i ee44 a1 1222"  

应作为输出:

"i ee(44) a(1) (1222)"

我正在尝试使用String.replace实现它(a,b)但没有成功。

I am trying to implement it using String.replace(a,b) but with no success.

推荐答案

"i ee44 a1 1222".replaceAll("\\d+", "($0)");

试试这个,看它是否有效。

Try this and see if it works.

由于您需要使用正则表达式,您可以考虑使用 replaceAll 而不是替换

Since you need to work with regular expressions, you may consider using replaceAll instead of replace.

这篇关于Java,如何替换字符串中的数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆