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 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
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());
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
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"]);
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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".
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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: 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,...
|
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...
| |