Formatting strings in VB.NET

R

RichardF

I have seen lots of information in help and in here about formatting
numbers and dates, but what about strings?

In VB6 I could do something like this...

Format("abcdefghi","@@@-@@-@@@@")

....and the result would be...

abc-de-fghi

How can I do this in VB.NET? I have looked at .ToString, and it just
referes to numeric formats and data time formats. I can't find how to
format strings.

Thanks for any help.

RichardF
 
P

Paul Clement

¤ I have seen lots of information in help and in here about formatting
¤ numbers and dates, but what about strings?
¤
¤ In VB6 I could do something like this...
¤
¤ Format("abcdefghi","@@@-@@-@@@@")
¤
¤ ...and the result would be...
¤
¤ abc-de-fghi
¤
¤ How can I do this in VB.NET? I have looked at .ToString, and it just
¤ referes to numeric formats and data time formats. I can't find how to
¤ format strings.

Here is one way it can be done:

Dim StringVal As String = "abcdefghi"
Dim FormattedStringVal As String
FormattedStringVal = StringVal.Format("{0}-{1}-{2}", StringVal.Substring(0, 3),
StringVal.Substring(3, 2), StringVal.Substring(5, 4))


Paul
~~~~
Microsoft MVP (Visual Basic)
 
R

RichardF

That would work, but that woud mean coding for every possible
variation of format mask or rewriting the Format function myself in
VB.NET.

This was something that was so easy in VB6, surely there must be a way
to do it in .NET too!

RichardF
 

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

Top