473,387 Members | 1,603 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,387 software developers and data experts.

String formatting

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 2836
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:wf*****************************@40tude.net...
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 #2
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]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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:wf*****************************@40tude.net...
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 #3
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:
Dim strNumber as string = "08701218300"
Dim strFormattedNumber as String = CLng(strNumber).ToString("0000 000 0000")
http://msdn.microsoft.com/library/de...ngoverview.asp

http://msdn.microsoft.com/library/de...matstrings.asp
Hope this helps
Jay

"AndyBarker" <an*********@icm-computer.co.uk> wrote in message
news:40********@news.netserv.net... 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]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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:wf*****************************@40tude.net...
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 #4
On Tue, 20 Apr 2004 18:38:10 -0400, Ken Tucker [MVP] wrote:
Hi,

Dim strPhone As String = "9999999999"

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


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

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

Similar topics

5
by: Thomas Philips | last post by:
Consider the following simple dictionary e={1:'one', 2: 'two'} e >>>'one' However, If I attempt to print e using a formatted string print " %(1)s" %e, I get a KeyError: '1'
10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
5
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
1
by: schoedl | last post by:
Hello, we often compose strings via a ostringstream and then create a string from it. What is the rationale of not being able to use string in place of a ostringstream, so I could write ...
6
by: Jack | last post by:
Hi there, Given a standard .NET string, does anyone know what the regular expression would be to locate each (optional) formatting item in the string (or more likely does anyone have a link that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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 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.