我有一个值为“3D7H40M20S”的字符串 哪个应该转换为3天7小时40分钟和20秒“。 这是我到目前为止所尝试的:
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
AttributeBinding attr = (AttributeBinding)bindings.getControlBinding("ScreeningSLAWaitTimeDuration");
scrSlaWaitDur = (String)attr.getInputValue();
System.out.println("-------------------------------------------SCREENING SLA WAIT DURATION---------------"+scrSlaWaitDur);
scrSlaWaitDur = scrSlaWaitDur.substring(2, scrSlaWaitDur.length());
System.out.println("--------------------------------------------SUBSTRING--------------------"+scrSlaWaitDur);
int dIndex = scrSlaWaitDur.indexOf("D");
System.out.println("*******************************************INDEX OF D**********************************"+dIndex);
if(dIndex != -1){
String newDur = scrSlaWaitDur.substring(0, dIndex)+" days "+scrSlaWaitDur.substring(dIndex+1);
int mIndex = scrSlaWaitDur.lastIndexOf("M");
String newDur2 = scrSlaWaitDur.substring(0, mIndex)+" minute "+scrSlaWaitDur.substring(mIndex+1);
int sIndex = newDur2.lastIndexOf("S");
String newDur3 = newDur2.substring(0,sIndex)+" second";
scrSlaWaitDur = newDur3;
return scrSlaWaitDur;
}
我知道这很烦人且非常冗长。 我能以更简单的方式达到要求吗?
答案 0 :(得分:2)
务实的方法是取代单位代币:
return scrSlaWaitDur.replace("D", " Days ").replace("H", " Hours ").replace("M", " Minutes ").replace("S", " Seconds");
输出:
3 Days 7 Hours 40 Minutes 20 Seconds
答案 1 :(得分:1)
您可以使用不带index()的substring(),即:
String result =
scrSlaWaitDur.replace("D"," Days ").replace("H"," Hours ").replace("M"," Minutes ").replace("S"," Seconds");
但我相信有人会想出一个fency正则表达式解决方案。 ; - )