473,406 Members | 2,220 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,406 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 85454

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.