开发者

Fastest way to format a String using concatenation and manipulation

开发者 https://www.devze.com 2023-04-11 19:30 出处:网络
String temp = \"\"; temp = String.format(\"%02d\", \"\"+hour)+\":\"+String.format(\"%02d\", \"\"+min)+\":\"+String.format(\"%02d\", \"\"+sec);
String temp = "";

temp = String.format("%02d", ""+hour)+":"+String.format("%02d", ""+min)+":"+String.format("%02d", ""+sec);

Is this the fastest way to format the numbers with concatenation to specify leading zeroes, or is there another way?

I have to display the String in the format 00:00:00. The hour, min, and sec are int va开发者_如何学Pythonriables which start from 0 and are put into the Thread which counts the time elapsed and displays this time accordingly. Is this the correct way, or is there any better way?


Try this. I assume that hour, min and sec are ints.

String temp = "";
temp = String.format("%02d:%02d:%02d", hour, min, sec);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号