Shaitan00 Posted August 10, 2008 Posted August 10, 2008 There are a bunch of places where I use a string value such as "6" or "10" where I want to display it as "000006" or "000010" (6 characters, 0 padded). Within a GridView [asp.net 2.0] this is easy as I simply use the DataFormatString {0:000000}, but how can this be achieved for a normal string that I use to populate a DrillDown or Label witin a .cs file? How can I pad a string with "0"'s so that it is always X characters long? Any help would be greatly appreciated... Thanks, Quote
Administrators PlausiblyDamp Posted August 10, 2008 Administrators Posted August 10, 2008 [PLAIN]Re: How to pad a string with zero's so that "6" is "000006" [C# 2005][/PLAIN] As a quick example string s = "6"; s = s.PadLeft(6, '0'); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MrPaul Posted August 12, 2008 Posted August 12, 2008 string.Format If you wish to perform the conversion from a numeric data type to a padded string with one operation: string str = string.Format("{0:d6}", number); Good luck :cool: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.