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.

Go Back  Xtreme .NET Talk > .NET > General > How to pad a string with zero's so that "6" is "000006" [C# 2005]


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

Preferred language:
C#
 
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 [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,
Reply With Quote
  #2  
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,461
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
Code:
string s = "6";
s =  s.PadLeft(6, '0');
__________________
Posting Guidelines FAQ Post Formatting

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

Preferred language:
C#
 
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:

Code:
string str = string.Format("{0:d6}", number);

Good luck
__________________
Never trouble another for what you can do for yourself.
Reply With Quote
Reply

Bookmarks

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 08: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 10:36 AM
"Sub Main" was not found in "Program.form1"... CryoEnix General 2 12-30-2003 09:56 AM

Advertisement:







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.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
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..
subscribe