Delphi字符串长度不够前面补0,该如何处理

字符串长度不够前面补0
str := '29'; 我想要str长度为5位,什么方法可以前面补0 ?

分享到:更多


------解决方案--------------------
才5位..自己加拉..

str := rightstr('00000' + str, 5)
------解决方案--------------------
1.如2楼的写法,先拼上五个0后右截取;
2.写个循环判断长度小于五位就左补0的;
3.Format('%.5d',[strToInt(str)]) ,前提是str能转换成整数.
------解决方案--------------------

<span style="color:#000000">

uses System.StrUtils;



var

  str: string;

begin

  str := '34';

  str := Format('%5s', [str]);       //格式化字符串,用空格式填充

  str := ReplaceStr(str, ' ', '0');  //将空格替代换0

  ShowMessage(str);

</span>


------解决方案--------------------
uses
  strutils;

...
str := dupestring('0',5-length(str))  +str;
------解决方案--------------------
Format('%5.5d',[StrToInt(s)]);

发布了58 篇原创文章 · 获赞 10 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/xyzhan/article/details/94397442