473,324 Members | 2,179 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,324 software developers and data experts.

Help formating string

Opa
I have a string with n number of characters. I'm trying to create another string which is exactly 12 characters having leadings zeros and the input string at the end

ex: string1 = "7344", I want to format string1 such that it results another string with string1 at the end and leading zeros in front of it like: "000000007344

so I try string2=String.Format(??????,string1

does anyone know the correct formatting expression

Thank
Nov 16 '05 #1
4 1819
I think you'll need to cast it to an int first so

int x = string.Parse(string1);//Must be able to be parsed or exception will
be thrown.

MessageBox.Show(x.ToString("000000000000"));

This will cause it to be 12 long with the difference between the lengths
shown in 0's
"Opa" <an*******@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
I have a string with n number of characters. I'm trying to create another string which is exactly 12 characters having leadings zeros and the input
string at the end.
ex: string1 = "7344", I want to format string1 such that it results another string with string1 at the end and leading zeros in front of it
like: "000000007344"
so I try string2=String.Format(??????,string1)

does anyone know the correct formatting expression?

Thanks

Nov 16 '05 #2
MS
I suggest you should rather us
string2=string1.PadLeft(12,'0');
Nov 16 '05 #3
Opa
Thanks guys

this works.
Nov 16 '05 #4
While using PadLeft, as others have suggested, is a more elegant solution,
you COULD use string.Format as follows:

string s1 = "7344";
string s2 = string.Format( "{0:000000000000}", int.Parse( s1 ));

The result would be the same...

Barry

"Opa" <an*******@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
I have a string with n number of characters. I'm trying to create another string which is exactly 12 characters having leadings zeros and the input
string at the end.
ex: string1 = "7344", I want to format string1 such that it results another string with string1 at the end and leading zeros in front of it
like: "000000007344"
so I try string2=String.Format(??????,string1)

does anyone know the correct formatting expression?

Thanks

Nov 16 '05 #5

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

Similar topics

0
by: sj | last post by:
Im trying to read sample values from aiff files but can not seem to get the unpacking right. Ths following code reads n frames from a sample file and prints the amplitudes to the terminal. In the...
13
by: yy0127 | last post by:
I don't know why i entered the below code and it will miss some records. Anyone can help me??? users = {} users1 = {} while 1: user, serviceType, msgType, inOut, date, time, numBytes =
17
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT if you choose the 9.95 item #5 BY ITSELF the total...
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...
2
by: sitko | last post by:
Hi, I'm in the process of converting a VB.net program into a C program so it can run on a unix like machine. I've been moving along at a nice pace, but this conversion has stumped me. I need...
1
by: nico | last post by:
Hi, I need to do a lot of string formating, and I have strings and/or unicode strings and when I do the following: "%s %s" % (u'Salut', 'H\xe4llo'), I get an exception : UnicodeDecodeError:...
9
nine72
by: nine72 | last post by:
Ok, so I have figured out how to parse an custom returned XML doc (actually a string that I made into a doc for testing). At this point I am attempting to integrate my parse routine into my main...
9
by: =?Utf-8?B?TmVpbEdvdHQ=?= | last post by:
I am trying to format a decimal number such as 0.0567 but forgot how to do formatting in C#. I tried the old C format escape sequence "\8.3f" the old C style but does not work. example:...
1
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.