第一段:对 %f 填充值
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package makewar;
imp
ort org.apache.commons.lang3.text.StrBuilder;
import org.apache.commons.lang3.text.StrTokenizer;
/**
* @author 何险峰
* 2013-02-01
*/
public class OWLStrTokenizer {
public static void main(String[] args) throws Exception {
String exp = "(暴雨预警12小时 value %f) and (暴雨预警6小时 value %f) and ( 暴雨预警3小时 value %f )";
String exp1 = exp.replace("(", " ( ");
String exp2 = exp1.replace(")", " ) ");
StrTokenizer st = new StrTokenizer(exp2);
String[] ts = st.getTokenArray();
for (String s : ts) {
System.out.println(s);
}
System.out.println("======================================================================");
for (int i = 0; i < ts.length; i++) {
String s = ts[i];
if (s.indexOf("%f")>=0){
ts[i] = new Double(Math.random()).toString()+"f";
} else {
if (ts[i].indexOf("(")<0){
ts[i]=ts[i]+" ";
}
}
}
StrBuilder sb = new StrBuilder();
sb = sb.appendAll(ts);
String s= sb.toString();
System.out.println(s);
}
}
run:
(
暴雨预警12小时
value
%f
)
and
(
暴雨预警6小时
value
%f
)
and
(
暴雨预警3小时
value
%f
)
======================================================================
(暴雨预警12小时 value 0.30153450592063424f) and (暴雨预警6小时 value 0.07835461977926894f) and (暴雨预警3小时 value 0.8277326732056489f)
成功构建 (总时间: 0 秒)
-------------------------------------------------------------
第二段: 使用groovy填值
list=[0,1,2,3,4,5];
s = "hxf";
"${s} : ${list[3]} : ${list.size()}"
------------------------------------------------------------
Result : hxf : 3 : 6
------------------------------------------------------------
第三段:使用format填值
package testgroovy;
/**
*
* @author 何险峰
* 2013-02-04
*/
public class FillVal {
public static void formatFill(){
Float[] ff={0.0f,1.1f,2.2f,3.3f,4.4f,5.5f};
String fmt = "(暴雨预警12小时 value %1$5.2ff) and (暴雨预警6小时 value %4$5.2ff) and ( 暴雨预警3小时 value %2$5.2ff )";
String str = String.format(fmt, ff);
System.out.println(str);
}
public static void main(String[] args) throws Exception {
formatFill();
}
}
================================================================================
run:
(暴雨预警12小时 value 0.00f) and (暴雨预警6小时 value 3.30f) and ( 暴雨预警3小时 value 1.10f )
成功构建 (总时间: 0 秒)
评论