Formating a string

J

John Smith

What is the best way to format a string like

ABCDEFGHIJK

TO

ABC.DEF.GHI-JK

I found a lot of examples but I don't know which one is the best.....

thanks!
 
K

Kevin Spencer

Can you explain the rules?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
J

John Smith

Hello Kevin,
just put a . after the 3rd letter, one more . after the 6th letter and a -
after the 9th letter.

so, XXX.XXX.XXX-XX

thanks!
 
M

Mark Rae

just put a . after the 3rd letter, one more . after the 6th letter and a -
after the 9th letter.

so, XXX.XXX.XXX-XX

Is it always that way? Is the unformatted string always 11 characters in
length?

If so, this will do it:

string strUnformatted = "ABCDEFGHIJK";
string strFormatted
= strUnformatted.Substring(0, 3) + "."
+ strUnformatted.Substring(3, 3) + "."
+ strUnformatted.Substring(6, 3) + "-"
+ strUnformatted.Substring(9);

Create a class with a static function which takes the unformatted string as
a parameter and returns the formatted string.
 
J

John Smith

yes, it is aways 11 chars, so I should do this way then? using substring?

I thought there was a way using string.format or somthing....

thanks anyway!
 
K

Kevin Spencer

You could do it with String.Format, but Mark's method is easier, and just as
good.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top