在Java中,如何替换一段字符串中的数字abc为[2*a][2*b][2*c].如sdf789sdf4为sdf[14][16][18]sdf[8]
展开全部
package com;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Marik
{
public static void main(String[] args)
{
String str = "sdf789sdf4";
String result = "";
Pattern pattern = Pattern.compile("(\\d)");
Matcher matcher = pattern.matcher(str);
int start = 0;
while(matcher.find())
{
int tmp = Integer.parseInt(matcher.group(1)) * 2;
result += str.substring(start, matcher.start()) + "[" + tmp + "]";
start = matcher.start() + 1;
}
System.out.println(result);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询