473,236 Members | 1,771 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,236 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 85411

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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.