473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
ICustomFormatte r interfaces. I can use this class with String.Format like
so:

s = String.Format(N ew SubFormat(),"({ 0:S0,3}) {0:S3,3}-{0:S6,4}",strPh one)

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 2853
Hi,

Dim strPhone As String = "9999999999 "

Dim strFormatedNumb er As String = CLng(strPhone). ToString("(###) ###-####")
Debug.WriteLine (strFormatedNum ber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunc hmeat_sbcglobal .net> wrote in message
news:wf******** *************** ******@40tude.n et...
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
ICustomFormatte r interfaces. I can use this class with String.Format like
so:

s = String.Format(N ew SubFormat(),"({ 0:S0,3}) {0:S3,3}-{0:S6,4}",strPh one)

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 = "0870121830 0"
Dim strFormattedNum ber as String = CDbl(strNumber) .ToString("#### ### ####")
would return 870 121 8300

Regards

Andy
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi,

Dim strPhone As String = "9999999999 "

Dim strFormatedNumb er As String = CLng(strPhone). ToString("(###) ###-####") Debug.WriteLine (strFormatedNum ber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunc hmeat_sbcglobal .net> wrote in message
news:wf******** *************** ******@40tude.n et...
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 ICustomFormatte r interfaces. I can use this class with String.Format like so:

s = String.Format(N ew SubFormat(),"({ 0:S0,3}) {0:S3,3}-{0:S6,4}",strPh one)
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 = "0870121830 0"
Dim strFormattedNum ber 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*********@ic m-computer.co.uk> wrote in message
news:40******** @news.netserv.n et... What if the phone number begins with a 0 like most do here in the uk.

Dim strNumber as string = "0870121830 0"
Dim strFormattedNum ber as String = CDbl(strNumber) .ToString("#### ### ####") would return 870 121 8300

Regards

Andy
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi,

Dim strPhone As String = "9999999999 "

Dim strFormatedNumb er As String = CLng(strPhone). ToString("(###) ###-####")
Debug.WriteLine (strFormatedNum ber)

Ken
-------------------
"Chris Dunaway" <dunawayc@_lunc hmeat_sbcglobal .net> wrote in message
news:wf******** *************** ******@40tude.n et...
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 ICustomFormatte r interfaces. I can use this class with String.Format like so:

s = String.Format(N ew SubFormat(),"({ 0:S0,3}) {0:S3,3}-{0:S6,4}",strPh one)
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 strFormatedNumb er As String = CLng(strPhone). ToString("(###) ###-####")
Debug.WriteLine (strFormatedNum ber)


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
3021
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
4840
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 operation would grow the string beyond its maximum size, an exeception would be thrown. This kind of string obviously has superior performance over a std::string because there's never any additional memory-allocation. But before I'm going to re-invent...
20
11320
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 expressions and I sure there is a lot ways, but I need realy efficient one
5
6173
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 only supposed to use that only when you have an xml ~file~... not an XML string (my XML is coming from a database field). Is there a cut & dry example out there on how to transform a simple XML string using XSL? -AC
4
2436
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 unfortunate that i have to walk via System.String to do that. For instance, from an integer of value 123, i want:
7
398
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 other country. When I retrieve the phone numbers, I need to display them as (###) ###-#### x 99999 if it is a Canadian number or (###) ###-#### Ext. 99999 if it is US phone number. Potentially I'd have other countries added as well which might have...
7
3112
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
4349
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 unformatted data values (as decimals) just fine, so I know there's not a problem with the data retrieval, just the formatting. I have read that this would work: lblPrice.Text = prodRow.ToString("C");
1
8248
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 string str; str << ... << ...; SomeAPI( str.c_str() );
6
1698
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 will show me this). For instance, given the following simple string: "My phone number is {0} and my SSN is {1}" I want to enumerate (or create a collection of) all formatting items in the
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.