将零填充添加到字符串

如何为字符串添加“ 0”填充,以使字符串长度始终为4?



If input "1", 3 padding is added = 0001

If input "25", 2 padding is added = 0025

If input "301", 1 padding is added = 0301

If input "4501", 0 padding is added = 4501


人到中年有点甜
浏览 324回答 4
4回答

侃侃尔雅

您可以使用PadLeftvar newString = Your_String.PadLeft(4, '0');

肥皂起泡泡

myInt.ToString("D4");

慕神8447489

"1".PadLeft(4, '0');
打开App,查看更多内容
随时随地看视频慕课网APP