String里面double数值的提取处理
我有一段很长的String,里面有很多double型的数字,我想提取数字进行四舍五入到整数,然后它们在string里面的位置不改变生成新的string,要怎么做呀。下面是一段string的例子:
M 306.4681,396.68377 C 308.9431,396.10181 314.7881,395.47518 319.45698,395.29126 C 326.41469,395.01717 328.17041,394.61868 329.19072,393.08202 C 331.29318,389.91554 325.31174,382.46563 321.93162,384.04078 C 321.63656,384.17828 320.96156,383.61578 320.43162,382.79078 C 319.82772,381.85063 319.18817,381.66401 318.7181,382.29078 C 316.64896,385.04963 310.62077,386.26682 298.9681,386.27862 C 288.92054,386.2888 286.52066,386.59422 284.2181,388.15579 C 281.05321,390.30217 280.80137,391.31828 283.11355,392.61224 C 284.2926,393.27207 285.75401,392.85786 288.26926,391.15097 C 292.45142,388.31288 293.91488,388.8649 290.12076,391.84936 C 286.45702,394.73126 286.79679,395.79078 291.38469,395.79078 C 294.50441,395.79078 295.7086,395.23163 297.30319,393.04261 C 298.40423,391.53112 299.63901,390.62836 300.04713,391.03648 C 300.45525,391.4446 300.04193,392.60421 299.12863,393.61339 C 295.43699,397.69261 297.86246,398.70727 306.4681,396.68377 z M 319.7281,379.5214 C 320.4101,380.0488 321.78502,380.77233 322.78349,381.12925 C 324.24496,381.65168 324.47711,381.39455 323.9743,379.81034 C 323.02333,376.81408 326.20949,371.16745 331.31225,366.80573 C 333.834,364.65021 335.68819,362.75102 335.43267,362.58531 C 335.17716,362.4196 333.54632,361.28968 331.80859,360.07437 L 328.64908,357.86471 L 326.0077,361.32775 C 322.0897,366.46451 319.08343,366.05634 313.40492,359.61666 C 310.53884,356.3664 308.39328,354.71899 307.63591,355.18707 C 305.83028,356.30301 301.4681,365.38976 301.4681,368.03508 C 301.4681,369.60997 303.43648,372.08861 307.64907,375.81832 C 315.13032,382.442 316.71093,383.18216 317.7281,380.53808 C 318.26056,379.15397 318.85935,378.84958 319.7281,379.5214 z M 375.153,365.72716 C 379.59012,364.0507 383.18872,360.03525 389.64566,349.55571 C 397.67566,336.52311 402.61496,332.55741 414.2181,329.8268 C 417.1056,329.14727 419.4681,328.20657 419.4681,327.73635 C 419.4681,325.92682 412.63787,325.69137 406.40594,327.28608 C 398.11657,329.40727 390.98325,333.43716 383.97963,339.95556 C 375.31515,348.01977 373.03355,349.11983 366.24832,348.50457 C 360.89729,348.01935 360.4681,348.13446 360.4681,350.05475 C 360.4681,351.19562 359.11982,354.51808 357.47193,357.438 L 354.47575,362.74693 L 358.22193,364.72466 C 362.40619,366.93367 370.66538,367.4227 375.153,365.72716 z
就贴这些吧,string可能远比这个要长。里面的数字都四舍五入到整形,然后变成新的string。
[解决办法]
String s="M 306.4681,396.68377 C 308.9431,396.10181 314.7881,395.47518 319.45698,395.29126 C 326.41469,395.01717 328.17041,394.61868 329.19072,393.08202 C 331.29318,389.91554 325.31174,382.46563 321.93162,384.04078 C 321.63656,384.17828 320.96156,383.61578 320.43162,382.79078 C 319.82772,381.85063 319.18817,381.66401 318.7181,382.29078 C 316.64896,385.04963 310.62077,386.26682 298.9681,386.27862 C 288.92054,386.2888 286.52066,386.59422 284.2181,388.15579 C 281.05321,390.30217 280.80137,391.31828 283.11355,392.61224 C 284.2926,393.27207 285.75401,392.85786 288.26926,391.15097 C 292.45142,388.31288 293.91488,388.8649 290.12076,391.84936 C 286.45702,394.73126 286.79679,395.79078 291.38469,395.79078 C 294.50441,395.79078 295.7086,395.23163 297.30319,393.04261 C 298.40423,391.53112 299.63901,390.62836 300.04713,391.03648 C 300.45525,391.4446 300.04193,392.60421 299.12863,393.61339 C 295.43699,397.69261 297.86246,398.70727 306.4681,396.68377 z M 319.7281,379.5214 C 320.4101,380.0488 321.78502,380.77233 322.78349,381.12925 C 324.24496,381.65168 324.47711,381.39455 323.9743,379.81034 C 323.02333,376.81408 326.20949,371.16745 331.31225,366.80573 C 333.834,364.65021 335.68819,362.75102 335.43267,362.58531 C 335.17716,362.4196 333.54632,361.28968 331.80859,360.07437 L 328.64908,357.86471 L 326.0077,361.32775 C 322.0897,366.46451 319.08343,366.05634 313.40492,359.61666 C 310.53884,356.3664 308.39328,354.71899 307.63591,355.18707 C 305.83028,356.30301 301.4681,365.38976 301.4681,368.03508 C 301.4681,369.60997 303.43648,372.08861 307.64907,375.81832 C 315.13032,382.442 316.71093,383.18216 317.7281,380.53808 C 318.26056,379.15397 318.85935,378.84958 319.7281,379.5214 z M 375.153,365.72716 C 379.59012,364.0507 383.18872,360.03525 389.64566,349.55571 C 397.67566,336.52311 402.61496,332.55741 414.2181,329.8268 C 417.1056,329.14727 419.4681,328.20657 419.4681,327.73635 C 419.4681,325.92682 412.63787,325.69137 406.40594,327.28608 C 398.11657,329.40727 390.98325,333.43716 383.97963,339.95556 C 375.31515,348.01977 373.03355,349.11983 366.24832,348.50457 C 360.89729,348.01935 360.4681,348.13446 360.4681,350.05475 C 360.4681,351.19562 359.11982,354.51808 357.47193,357.438 L 354.47575,362.74693 L 358.22193,364.72466 C 362.40619,366.93367 370.66538,367.4227 375.153,365.72716 z";
String[] arr=s.split(",");//先按,分割
StringBuilder sb=new StringBuilder();
for(String str:arr){
String[] arr1=str.split("\\s+");//在用空格分割拿到每组字符串
for(String s1:arr1){
if(s1.matches("\\d+\\.\\d+")){//是小数的四舍五入后添加
sb.append(String.format("%.0f",Float.parseFloat(s1))).append(" ");
}else{//不是小数的原样添加
sb.append(s1).append(" ");
}
}
}
System.out.println(sb);