473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String Format padding with zeros

RSH

I have strings that are being converted that need to be eight characters
long. The values are coming in as anywhere between 1 and 8 characters long.
I need to pad the strings with zeros to make all IDs 8 characters long:

Example

Input number Need
1 00000001
121 00000121
10567 00010567
I tried this but it doesn not work, I simply get the same ID back (Example:
2)

sTemp = String.Format("{0:########}", DR["ID"].ToString());

What am I missing?

Thanks,

Ron
Jan 10 '06 #1
5 85450

RSH wrote:
I have strings that are being converted that need to be eight characters
long. The values are coming in as anywhere between 1 and 8 characters long.
I need to pad the strings with zeros to make all IDs 8 characters long:

Example

Input number Need
1 00000001
121 00000121
10567 00010567
I tried this but it doesn not work, I simply get the same ID back (Example:
2)

sTemp = String.Format("{0:########}", DR["ID"].ToString());

What am I missing?


sTemp = DR["ID"].ToString().PadLeft(8, "0"c);

--
Larry Lard
Replies to group please

Jan 10 '06 #2
I always find the number formatting stuff difficult to find in the
documentation, but if you start with String.Format you can eventually
find it here:

http://msdn.microsoft.com/library/de...matstrings.asp

The answer to your question appears to be this format string:

sTemp = String.Format("{0:00000000}", DR["ID"].ToString());

Jan 10 '06 #3

Larry Lard wrote:
RSH wrote:
I have strings that are being converted that need to be eight characters
long. The values are coming in as anywhere between 1 and 8 characters long.
I need to pad the strings with zeros to make all IDs 8 characters long:

Example

Input number Need
1 00000001
121 00000121
10567 00010567
I tried this but it doesn not work, I simply get the same ID back (Example:
2)

sTemp = String.Format("{0:########}", DR["ID"].ToString());

What am I missing?


sTemp = DR["ID"].ToString().PadLeft(8, "0"c);


Which is some hellish mix of C# and VB.NET syntax, sorry. I meant of
course

sTemp = DR["ID"].ToString().PadLeft(8, '0');

--
Larry Lard
Replies to group please

Jan 10 '06 #4
Sorry. I didn't notice that ToString() there.

Assuming that DR["ID"] is a number, not a string, then the code should
be this:

sTemp = String.Format("{0:00000000}", DR["ID"]);

Jan 10 '06 #5
RSH
Thanks! Perfect.

I appreciate the quick response.

Ron
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Larry Lard wrote:
RSH wrote:
> I have strings that are being converted that need to be eight
> characters
> long. The values are coming in as anywhere between 1 and 8 characters
> long.
> I need to pad the strings with zeros to make all IDs 8 characters long:
>
> Example
>
> Input number Need
> 1 00000001
> 121 00000121
> 10567 00010567
>
>
> I tried this but it doesn not work, I simply get the same ID back
> (Example:
> 2)
>
> sTemp = String.Format("{0:########}", DR["ID"].ToString());
>
>
>
> What am I missing?


sTemp = DR["ID"].ToString().PadLeft(8, "0"c);


Which is some hellish mix of C# and VB.NET syntax, sorry. I meant of
course

sTemp = DR["ID"].ToString().PadLeft(8, '0');

--
Larry Lard
Replies to group please

Jan 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
5
by: raffelm | last post by:
I'm struggling to find a way to include long path names in a command line argument string that I have to build at runtime. I need to create a string like -o:"c:\my documents\my file.txt". ...
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
1
by: Remi THOMAS | last post by:
Hi, When you execute this line of code string script = string.Format("foreach (GroupDoc gdoc in {0}.SetGroup(\"{1}\") {\r\n", "p1", "p2"); You get System.FormatException was unhandled...
2
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have the following: String.Format("{0}", myNumber); What is {0} called? The reason I ask is that I want to look up how present myNumber as 2 digits in the string. For example, if myNumber is...
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.