Information Technology Solutions, Answers and Experts
Write an Article Ask a Question

String formatting

Chris Dunaway
P: n/a
Chris Dunaway
I have a table in the database with a phone number field. The phone number
is stored without any punctuation (e. g. 9995551234). I wish to take that
string and format it for display (e. g. (999) 555-1234).

I know that I can use the .substring method of the string class to get the
characters I want and format it:

Dim s As String

With strPhone
s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" & .Substring(6,4)
End With


While this works, it seems a bit lengthy plus, what if I need to use a
different substring format for other string that I want to display?

I created a class called SubFormat that implements the IFormatProvider and
ICustomFormatter interfaces. I can use this class with String.Format like
so:

s = String.Format(New SubFormat(),"({0:S0,3}) {0:S3,3}-{0:S6,4}",strPhone)

The "S" is my custom format specificer. The 0,3 means start at index 0 in
the string and get the next 3 characters.

This also works and it can work with any string and format I wish.

My question (after that long post) is if I have re-invented the wheel.
Does this sort of functionality already exist?

I couldn't find it.

Thanks,


--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #1

4 Replies



Ken Tucker [MVP]
P: n/a
Ken Tucker [MVP]

re: String formatting

Hi,

Dim strPhone As String = "9999999999"

Dim strFormatedNumber As String = CLng(strPhone).ToString("(###) ###-####")
Debug.WriteLine(strFormatedNumber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
news:wfp5zwo0xcv2$.3024n5vkon65$.dlg@40tude.net...[color=blue]
>I have a table in the database with a phone number field. The phone number
> is stored without any punctuation (e. g. 9995551234). I wish to take that
> string and format it for display (e. g. (999) 555-1234).
>
> I know that I can use the .substring method of the string class to get the
> characters I want and format it:
>
> Dim s As String
>
> With strPhone
> s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" & .Substring(6,4)
> End With
>
>
> While this works, it seems a bit lengthy plus, what if I need to use a
> different substring format for other string that I want to display?
>
> I created a class called SubFormat that implements the IFormatProvider and
> ICustomFormatter interfaces. I can use this class with String.Format like
> so:
>
> s = String.Format(New SubFormat(),"({0:S0,3}) {0:S3,3}-{0:S6,4}",strPhone)
>
> The "S" is my custom format specificer. The 0,3 means start at index 0 in
> the string and get the next 3 characters.
>
> This also works and it can work with any string and format I wish.
>
> My question (after that long post) is if I have re-invented the wheel.
> Does this sort of functionality already exist?
>
> I couldn't find it.
>
> Thanks,
>
>
> --
> Chris
>
> To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
> address.[/color]


Nov 20 '05 #2

AndyBarker
P: n/a
AndyBarker

re: String formatting

What if the phone number begins with a 0 like most do here in the uk.

Dim strNumber as string = "08701218300"
Dim strFormattedNumber as String = CDbl(strNumber).ToString("#### ### ####")
would return 870 121 8300

Regards

Andy


"Ken Tucker [MVP]" <vb2ae@bellsouth.net> wrote in message
news:%23kQDaiyJEHA.3596@tk2msftngp13.phx.gbl...[color=blue]
> Hi,
>
> Dim strPhone As String = "9999999999"
>
> Dim strFormatedNumber As String = CLng(strPhone).ToString("(###)[/color]
###-####")[color=blue]
> Debug.WriteLine(strFormatedNumber)
>
> Ken
> -------------------
> "Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
> news:wfp5zwo0xcv2$.3024n5vkon65$.dlg@40tude.net...[color=green]
> >I have a table in the database with a phone number field. The phone[/color][/color]
number[color=blue][color=green]
> > is stored without any punctuation (e. g. 9995551234). I wish to take[/color][/color]
that[color=blue][color=green]
> > string and format it for display (e. g. (999) 555-1234).
> >
> > I know that I can use the .substring method of the string class to get[/color][/color]
the[color=blue][color=green]
> > characters I want and format it:
> >
> > Dim s As String
> >
> > With strPhone
> > s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" &[/color][/color]
..Substring(6,4)[color=blue][color=green]
> > End With
> >
> >
> > While this works, it seems a bit lengthy plus, what if I need to use a
> > different substring format for other string that I want to display?
> >
> > I created a class called SubFormat that implements the IFormatProvider[/color][/color]
and[color=blue][color=green]
> > ICustomFormatter interfaces. I can use this class with String.Format[/color][/color]
like[color=blue][color=green]
> > so:
> >
> > s = String.Format(New SubFormat(),"({0:S0,3})[/color][/color]
{0:S3,3}-{0:S6,4}",strPhone)[color=blue][color=green]
> >
> > The "S" is my custom format specificer. The 0,3 means start at index 0[/color][/color]
in[color=blue][color=green]
> > the string and get the next 3 characters.
> >
> > This also works and it can work with any string and format I wish.
> >
> > My question (after that long post) is if I have re-invented the wheel.
> > Does this sort of functionality already exist?
> >
> > I couldn't find it.
> >
> > Thanks,
> >
> >
> > --
> > Chris
> >
> > To send me an E-mail, remove the underscores and lunchmeat from my[/color][/color]
E-Mail[color=blue][color=green]
> > address.[/color]
>
>[/color]


Nov 20 '05 #3

Jay B. Harlow [MVP - Outlook]
P: n/a
Jay B. Harlow [MVP - Outlook]

re: String formatting

Andy,
Look up "Numeric Format Strings" in the online help, specifically "Custom
Numeric Format Strings", you will see that # is a digit placeholder, while 0
is the zero placeholder, if you want zeros to appear in your formatted
string use 0 instead of #. Something like:
[color=blue]
> Dim strNumber as string = "08701218300"
> Dim strFormattedNumber as String = CLng(strNumber).ToString("0000 000[/color]
0000")


http://msdn.microsoft.com/library/de...ngoverview.asp

http://msdn.microsoft.com/library/de...matstrings.asp


Hope this helps
Jay

"AndyBarker" <andy.barker@icm-computer.co.uk> wrote in message
news:40865e73$1@news.netserv.net...[color=blue]
> What if the phone number begins with a 0 like most do here in the uk.
>
> Dim strNumber as string = "08701218300"
> Dim strFormattedNumber as String = CDbl(strNumber).ToString("#### ###[/color]
####")[color=blue]
> would return 870 121 8300
>
> Regards
>
> Andy
>
>
> "Ken Tucker [MVP]" <vb2ae@bellsouth.net> wrote in message
> news:%23kQDaiyJEHA.3596@tk2msftngp13.phx.gbl...[color=green]
> > Hi,
> >
> > Dim strPhone As String = "9999999999"
> >
> > Dim strFormatedNumber As String = CLng(strPhone).ToString("(###)[/color]
> ###-####")[color=green]
> > Debug.WriteLine(strFormatedNumber)
> >
> > Ken
> > -------------------
> > "Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> wrote in message
> > news:wfp5zwo0xcv2$.3024n5vkon65$.dlg@40tude.net...[color=darkred]
> > >I have a table in the database with a phone number field. The phone[/color][/color]
> number[color=green][color=darkred]
> > > is stored without any punctuation (e. g. 9995551234). I wish to take[/color][/color]
> that[color=green][color=darkred]
> > > string and format it for display (e. g. (999) 555-1234).
> > >
> > > I know that I can use the .substring method of the string class to get[/color][/color]
> the[color=green][color=darkred]
> > > characters I want and format it:
> > >
> > > Dim s As String
> > >
> > > With strPhone
> > > s = "(" & .Substring(0,3) & ") " & .SubString(3,3) & "-" &[/color][/color]
> .Substring(6,4)[color=green][color=darkred]
> > > End With
> > >
> > >
> > > While this works, it seems a bit lengthy plus, what if I need to use a
> > > different substring format for other string that I want to display?
> > >
> > > I created a class called SubFormat that implements the IFormatProvider[/color][/color]
> and[color=green][color=darkred]
> > > ICustomFormatter interfaces. I can use this class with String.Format[/color][/color]
> like[color=green][color=darkred]
> > > so:
> > >
> > > s = String.Format(New SubFormat(),"({0:S0,3})[/color][/color]
> {0:S3,3}-{0:S6,4}",strPhone)[color=green][color=darkred]
> > >
> > > The "S" is my custom format specificer. The 0,3 means start at index[/color][/color][/color]
0[color=blue]
> in[color=green][color=darkred]
> > > the string and get the next 3 characters.
> > >
> > > This also works and it can work with any string and format I wish.
> > >
> > > My question (after that long post) is if I have re-invented the wheel.
> > > Does this sort of functionality already exist?
> > >
> > > I couldn't find it.
> > >
> > > Thanks,
> > >
> > >
> > > --
> > > Chris
> > >
> > > To send me an E-mail, remove the underscores and lunchmeat from my[/color][/color]
> E-Mail[color=green][color=darkred]
> > > address.[/color]
> >
> >[/color]
>
>[/color]


Nov 20 '05 #4

Chris Dunaway
P: n/a
Chris Dunaway

re: String formatting

On Tue, 20 Apr 2004 18:38:10 -0400, Ken Tucker [MVP] wrote:
[color=blue]
> Hi,
>
> Dim strPhone As String = "9999999999"
>
> Dim strFormatedNumber As String = CLng(strPhone).ToString("(###) ###-####")
> Debug.WriteLine(strFormatedNumber)
>[/color]

I know that method works for phone numbers, but consider this contrived
example:

Suppose I had some sort of "Inventory Code" that was stored in the database
in a single field and sample data looks like this: 14AB225 and I wanted to
format a string so that a report reads like this:

"Item found in Warehouse 14, Room A, Row B, Shelf 2, Position 25"

I can't very well convert 14AB225 into a long and use string format. With
my custom formatter, I could specify a format string like this:

"Item found in Warehouse {0:S0,2}, Room {0:S2,1}, Row {0:S3,1}, Shelf
{0:S4,1}, Position {0:S5,2}"

As I said, my custom formatter can work this way. I just wanted to know if
this sort of functionality already existed.
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #5

Post your reply

Sign in to post your reply or Sign up for a free account.



Didn't find the answer to your question? Post your Visual Basic .NET question on Bytes

You can also browse similar questions: Visual Basic .NET

Get Visual Basic .NET Help

Get Visual Basic .NET help from a network of professionals.

Post your Question » Over 331,212 Members | 3356 Online