General Topics include: .NET Framework
Note: Most posts can be categorized into one of the forums below, where experts on that topic will be able to help you find a solution to your problem faster and more efficiently.

Enjoy an ad free experience by logging in. Not a member yet? Register.
Go Back  Xtreme .NET Talk > .NET > General > How to pad a string with zero's so that "6" is "000006" [C# 2005]

Thread Tools Display Modes
Old 08-10-2008, 01:29 AM
Shaitan00 Shaitan00 is offline
Junior Contributor

Preferred language:
Join Date: Aug 2003
Location: Hell
Posts: 358
Shaitan00 is on a distinguished road
Question How to pad a string with zero's so that "6" is "000006" [C# 2005]

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 [ 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...
Reply With Quote
Old 08-10-2008, 02:21 AM
PlausiblyDamp's Avatar
PlausiblyDamp PlausiblyDamp is offline
Ultimate Contributor

Preferred language:
C#, VB
Join Date: Sep 2002
Location: Lancashire, UK
Posts: 6,471
PlausiblyDamp is on a distinguished road
Default Re: How to pad a string with zero's so that "6" is "000006" [C# 2005]

As a quick example
string s = "6";
s =  s.PadLeft(6, '0');
Posting Guidelines FAQ Post Formatting

Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein
Reply With Quote
Old 08-12-2008, 03:53 PM
MrPaul's Avatar
MrPaul MrPaul is offline

Preferred language:
Join Date: Jun 2006
Location: Hampshire, UK
Posts: 428
MrPaul is on a distinguished road
Lightbulb 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
Never trouble another for what you can do for yourself.
Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
asp:checkbox insert a "1" or "0" for true or false HELP! bkedersha ASP.NET 2 10-07-2005 07:31 PM
What is "This Organization" SID AND "Selective Authentication"? favaedi Directory / File IO / Registry 0 11-21-2004 09:23 AM
CTYPE to "string" or "generic", doable?? eramgarden ASP.NET 0 08-27-2004 06:03 AM
"File | Close" -vs.- the exit "X" on form joe_pool_is Windows Forms 14 02-26-2004 11:36 AM
"Sub Main" was not found in "Program.form1"... CryoEnix General 2 12-30-2003 10:56 AM


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..