将零填充添加到string

如何添加“0”填充string,使我的string长度始终是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 

你可以使用PadLeft

 var newString = Your_String.PadLeft(4, '0'); 
 myInt.ToString("D4"); 
 string strvalue="11".PadRight(4, '0'); 

输出= 1100

 string strvalue="301".PadRight(4, '0'); 

输出= 3010

 string strvalue="11".PadLeft(4, '0'); 

输出= 0011

 string strvalue="301".PadLeft(4, '0'); 

输出= 0301

 "1".PadLeft(4, '0');