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

Format a text field

cj2
While I'm learning about format, will it work with strings?

I want to take this string
dim thisCustNo as string = "9128764526"
and make it look like
(912)876-4526

I found I can do this
thisCustNo.Format("({0}){1}-{2}",thisCustNo.Substring(0,3),thisCustNo.Substrin g(3,3),thisCustNo.Substring(6,4))

But for me that isn't a bit easier or clearer or nicer than
"("+thisCustNo.Substring(0,3)+")"+thisCustNo.Subst ring(3,3)+"-"+thisCustNo.Substring(6,4)

Am I missing something?
Sep 10 '08 #1
13 1045
Use whichever seems easier to you. Your second example is correct except for
the use of + instead of &. You could also examine regular expressions.

"cj2" <cj*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
While I'm learning about format, will it work with strings?

I want to take this string
dim thisCustNo as string = "9128764526"
and make it look like
(912)876-4526

I found I can do this
thisCustNo.Format("({0}){1}-{2}",thisCustNo.Substring(0,3),thisCustNo.Substrin g(3,3),thisCustNo.Substring(6,4))

But for me that isn't a bit easier or clearer or nicer than
"("+thisCustNo.Substring(0,3)+")"+thisCustNo.Subst ring(3,3)+"-"+thisCustNo.Substring(6,4)

Am I missing something?
Sep 10 '08 #2
Hi CJ,

Both solutions should work correctly. However, just as James pointed out,
in the second solution, we'd better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 11 '08 #3
Jeffrey,

I agree of course complete and it is much nicer to show the difference
between a string concat and a math operation like that is in VB possible.

However, the counting behaviour is only with Option Strict Off.

No reply needed, it is for the OP, I know very well that you know that.

:-)

Cor
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:ml**************@TK2MSFTNGHUB02.phx.gbl...
Hi CJ,

Both solutions should work correctly. However, just as James pointed out,
in the second solution, we'd better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.
Sep 11 '08 #4
cj2
Seems to me when I played with C# they didn't offer the & operator.
They used +, and so has every language I have used in the past. I kind
of like & for readability but have been considering going back to +
because it would facilitate my move to C# easier in the future.

Cor Ligthert[MVP] wrote:
Jeffrey,

I agree of course complete and it is much nicer to show the difference
between a string concat and a math operation like that is in VB possible.

However, the counting behaviour is only with Option Strict Off.

No reply needed, it is for the OP, I know very well that you know that.

:-)

Cor
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:ml**************@TK2MSFTNGHUB02.phx.gbl...
>Hi CJ,

Both solutions should work correctly. However, just as James pointed out,
in the second solution, we'd better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.
Sep 11 '08 #5
CJ

Do you want, I like the & more for string concatination, while I like the
index brackets in C# more then in VB the () for that.

However feel free to do as you want, but keep in mind that a program with +
for strings concatinations looks for every VB profesional as a program by an
amateur.

It is not true that every language uses a +. All C derived languages as
JavaScript, Java, C# and C++ use a +. But so it is as well with all from
Basic derived languages for the &.

Dit you ever try string concatination in JavaScript, you would wish you had
an & there.

Cpr
"cj2" <cj*@nospam.nospamschreef in bericht
news:uX**************@TK2MSFTNGP02.phx.gbl...
Seems to me when I played with C# they didn't offer the & operator. They
used +, and so has every language I have used in the past. I kind of like
& for readability but have been considering going back to + because it
would facilitate my move to C# easier in the future.

Cor Ligthert[MVP] wrote:
>Jeffrey,

I agree of course complete and it is much nicer to show the difference
between a string concat and a math operation like that is in VB possible.

However, the counting behaviour is only with Option Strict Off.

No reply needed, it is for the OP, I know very well that you know that.

:-)

Cor
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:ml**************@TK2MSFTNGHUB02.phx.gbl...
>>Hi CJ,

Both solutions should work correctly. However, just as James pointed
out,
in the second solution, we'd better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may
parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments
and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.
Sep 11 '08 #6
cj2


Cor Ligthert[MVP] wrote:
CJ

Do you want, I like the & more for string concatination, while I like
the index brackets in C# more then in VB the () for that.

However feel free to do as you want, but keep in mind that a program
with + for strings concatinations looks for every VB profesional as a
program by an amateur.
I strongly disagree. As mentioned C and C# etc use + and I'd highly
suspect that programmers moving from those languages are comfortable
using + and why change to &. Any serious programmer who's been around
any length of time knows + can be used to concatenate strings.
It is not true that every language uses a +.
I Never said every language uses +. I said "every language I have used
in the past". If VB up to VB4 had & I didn't know about it. I jumped
from VB4 to VB2003 so if VB5 or 6 added & I wouldn't know. In the past
I've also used COBOL, C, C++, dBase, Clipper and Pascal and don't
remember any of them using &.

All C derived languages as
JavaScript, Java, C# and C++ use a +. But so it is as well with all from
Basic derived languages for the &.

Dit you ever try string concatination in JavaScript, you would wish you
had an & there.
Nope, I don't do javascript.
Cpr
Of course, as you noted I'll do as I want and you are free to as well.
Thanks for you input.
>

"cj2" <cj*@nospam.nospamschreef in bericht
news:uX**************@TK2MSFTNGP02.phx.gbl...
>Seems to me when I played with C# they didn't offer the & operator.
They used +, and so has every language I have used in the past. I
kind of like & for readability but have been considering going back to
+ because it would facilitate my move to C# easier in the future.

Cor Ligthert[MVP] wrote:
>>Jeffrey,

I agree of course complete and it is much nicer to show the
difference between a string concat and a math operation like that is
in VB possible.

However, the counting behaviour is only with Option Strict Off.

No reply needed, it is for the OP, I know very well that you know that.

:-)

Cor
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschreef in bericht
news:ml**************@TK2MSFTNGHUB02.phx.gbl.. .
Hi CJ,

Both solutions should work correctly. However, just as James pointed
out,
in the second solution, we'd better use "&" operator for string
concatenation. "+" may cause unexpected result, for example, it may
parse
the string into number for arithmetic operation instead of string
operation.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your
comments and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no
rights.

Sep 11 '08 #7
CJ2 wrote:
<snip>
I want to take this string
dim thisCustNo as string = "9128764526"
and make it look like
(912)876-4526
<snip>

**If** the value can be converted to a number, you can use a custom
numeric format:

<code>
Dim thisCustNo As String = "9128764526"
Dim FormatedCode1 As String = Format( _
CLng(thisCustNo), "'('###')'###'-'####")
'Or
Dim FormatedCode2 As String = String.Format( _
"{0:'('###')'###'-'####}", _
CLng(thisCustNo))
</code>

Hope this helps.

Branco.
Sep 11 '08 #8
cj2
Actually that is a pretty good solution too. Thanks.

Branco Medeiros wrote:
CJ2 wrote:
<snip>
>I want to take this string
dim thisCustNo as string = "9128764526"
and make it look like
(912)876-4526
<snip>

**If** the value can be converted to a number, you can use a custom
numeric format:

<code>
Dim thisCustNo As String = "9128764526"
Dim FormatedCode1 As String = Format( _
CLng(thisCustNo), "'('###')'###'-'####")
'Or
Dim FormatedCode2 As String = String.Format( _
"{0:'('###')'###'-'####}", _
CLng(thisCustNo))
</code>

Hope this helps.

Branco.
Sep 11 '08 #9
My "Language Reference", which I presume is VB 1 since it doesn't have a
version number, does not include the & as a string concatenation operator,
but it was there in Version 3 (1993) and after, so it's been around for a
very long time. That's the reason for the suggestion that most VB
programmers would consider the use of + as indicating the programmer is a
recent starter with VB who has brought coding habits from another language.

"cj2" <cj*@nospam.nospamwrote in message
news:uB**************@TK2MSFTNGP03.phx.gbl...
>

snip >
I Never said every language uses +. I said "every language I have used in
the past". If VB up to VB4 had & I didn't know about it. I jumped from
VB4 to VB2003 so if VB5 or 6 added & I wouldn't know. In the past I've
also used COBOL, C, C++, dBase, Clipper and Pascal and don't remember any
of them using &.
Sep 11 '08 #10
cj2
That was worded a lot better. "consider the use of + as indicating the
programmer is a recent starter with VB who has brought coding habits
from another language." That might be the case although I haven't heard
anyone express that sentiment before. I really am wasting too much time
debating this so this will be my last post on this subject regardless of
further comments.

I would like to point out that many programmers I run into think of VB
as a lessor language for unskilled, entry level programmers. That comes
from the BASIC heritage of the language. Though VB has come a long way
they persist in that belief and in fact IS management here feels that way.

Also one could make the case that a programmer who uses & must not have
been exposed to too many languages in their careers. There are a lot of
us out there who started with languages well before VB came along and/or
have backgrounds in languages that don't use & that would naturally use
+ in VB. Why change? Because teachers teach using & in VB these days?
Come on, we used + for so long and nobody found it confusing do I need
to cater to those to new to understand +?

As for me, I have to admit I think folks that are very insistent that
something like & is the correct way, or that option strict is absolutely
necessary, etc are probably pretty new to programming. I bet they are
actually quite skilled and knowledgeable about VB right now. Probably
way more than me. But they are only spouting what they've been taught.
After they've been in the field for awhile and had to learn a new
completely different language or two they might not see these small
things so black and white that they feel the need to impress their ways
of doing things on others so strongly.

Anyway, I don't mean to tick anyone off. Like I said I think many here
know VB much better than I. Just saying there are other ways to see
things and something little like that--anything more than a mention that
& is also available does little to impress me. I had actually welcomed
& when I found it in VS 2003 but my constantly trying to use it in a C#
project recently frustrated me greatly so I might be heading back to +
even in VB so my brain has one less thing to remember as I move back and
forth between languages.
James Hahn wrote:
My "Language Reference", which I presume is VB 1 since it doesn't have a
version number, does not include the & as a string concatenation
operator, but it was there in Version 3 (1993) and after, so it's been
around for a very long time. That's the reason for the suggestion that
most VB programmers would consider the use of + as indicating the
programmer is a recent starter with VB who has brought coding habits
from another language.

"cj2" <cj*@nospam.nospamwrote in message
news:uB**************@TK2MSFTNGP03.phx.gbl...
>>

snip >
I Never said every language uses +. I said "every language I have
used in the past". If VB up to VB4 had & I didn't know about it. I
jumped from VB4 to VB2003 so if VB5 or 6 added & I wouldn't know. In
the past I've also used COBOL, C, C++, dBase, Clipper and Pascal and
don't remember any of them using &.
Sep 12 '08 #11
On Fri, 12 Sep 2008 10:25:35 -0400, cj2 <cj*@nospam.nospamwrote:

>As for me, I have to admit I think folks that are very insistent that
something like & is the correct way, or that option strict is absolutely
necessary, etc are probably pretty new to programming. I bet they are
actually quite skilled and knowledgeable about VB right now. Probably
way more than me. But they are only spouting what they've been taught.
After they've been in the field for awhile and had to learn a new
completely different language or two they might not see these small
things so black and white that they feel the need to impress their ways
of doing things on others so strongly.
I have been programming for over 35 years. I use OPTION STRICT ON not
because I think it is absolutely necessary or because someone told me
it is something I should do, but because it saves me from making hard
to find mistakes.
Sep 12 '08 #12
You are not using Option Strict ON and are wondering why management regards
VB as a toy language?

We will have to agree to disagree, because I would never consider other
language syntax in deciding whether or not to use deprecated operators.
Perhaps that's due to the time I spent using Pro-IV - a language so similar
to VB that it required constant caution to not get the syntax confused. In
fact, I suspect that it is exposure to a variety of other languages rather
than a lack of familiarity that disposes one towards pedantry in syntax.

"cj2" <cj*@nospam.nospamwrote in message
news:e7**************@TK2MSFTNGP03.phx.gbl...
That was worded a lot better. "consider the use of + as indicating the
programmer is a recent starter with VB who has brought coding habits from
another language." That might be the case although I haven't heard anyone
express that sentiment before. I really am wasting too much time debating
this so this will be my last post on this subject regardless of further
comments.

I would like to point out that many programmers I run into think of VB as
a lessor language for unskilled, entry level programmers. That comes from
the BASIC heritage of the language. Though VB has come a long way they
persist in that belief and in fact IS management here feels that way.

Also one could make the case that a programmer who uses & must not have
been exposed to too many languages in their careers. There are a lot of
us out there who started with languages well before VB came along and/or
have backgrounds in languages that don't use & that would naturally use +
in VB. Why change? Because teachers teach using & in VB these days? Come
on, we used + for so long and nobody found it confusing do I need to cater
to those to new to understand +?

As for me, I have to admit I think folks that are very insistent that
something like & is the correct way, or that option strict is absolutely
necessary, etc are probably pretty new to programming. I bet they are
actually quite skilled and knowledgeable about VB right now. Probably way
more than me. But they are only spouting what they've been taught. After
they've been in the field for awhile and had to learn a new completely
different language or two they might not see these small things so black
and white that they feel the need to impress their ways of doing things on
others so strongly.

Anyway, I don't mean to tick anyone off. Like I said I think many here
know VB much better than I. Just saying there are other ways to see
things and something little like that--anything more than a mention that &
is also available does little to impress me. I had actually welcomed &
when I found it in VS 2003 but my constantly trying to use it in a C#
project recently frustrated me greatly so I might be heading back to +
even in VB so my brain has one less thing to remember as I move back and
forth between languages.
James Hahn wrote:
>My "Language Reference", which I presume is VB 1 since it doesn't have a
version number, does not include the & as a string concatenation
operator, but it was there in Version 3 (1993) and after, so it's been
around for a very long time. That's the reason for the suggestion that
most VB programmers would consider the use of + as indicating the
programmer is a recent starter with VB who has brought coding habits from
another language.

"cj2" <cj*@nospam.nospamwrote in message
news:uB**************@TK2MSFTNGP03.phx.gbl...
>>>

snip >
I Never said every language uses +. I said "every language I have used
in the past". If VB up to VB4 had & I didn't know about it. I jumped
from VB4 to VB2003 so if VB5 or 6 added & I wouldn't know. In the past
I've also used COBOL, C, C++, dBase, Clipper and Pascal and don't
remember any of them using &.
Sep 12 '08 #13
Such a long message, what is in fact a farm of dead horses in this
newsgroup)

:-)

Cor
"cj2" <cj*@nospam.nospamschreef in bericht
news:e7**************@TK2MSFTNGP03.phx.gbl...
That was worded a lot better. "consider the use of + as indicating the
programmer is a recent starter with VB who has brought coding habits from
another language." That might be the case although I haven't heard anyone
express that sentiment before. I really am wasting too much time debating
this so this will be my last post on this subject regardless of further
comments.

I would like to point out that many programmers I run into think of VB as
a lessor language for unskilled, entry level programmers. That comes from
the BASIC heritage of the language. Though VB has come a long way they
persist in that belief and in fact IS management here feels that way.

Also one could make the case that a programmer who uses & must not have
been exposed to too many languages in their careers. There are a lot of
us out there who started with languages well before VB came along and/or
have backgrounds in languages that don't use & that would naturally use +
in VB. Why change? Because teachers teach using & in VB these days? Come
on, we used + for so long and nobody found it confusing do I need to cater
to those to new to understand +?

As for me, I have to admit I think folks that are very insistent that
something like & is the correct way, or that option strict is absolutely
necessary, etc are probably pretty new to programming. I bet they are
actually quite skilled and knowledgeable about VB right now. Probably way
more than me. But they are only spouting what they've been taught. After
they've been in the field for awhile and had to learn a new completely
different language or two they might not see these small things so black
and white that they feel the need to impress their ways of doing things on
others so strongly.

Anyway, I don't mean to tick anyone off. Like I said I think many here
know VB much better than I. Just saying there are other ways to see
things and something little like that--anything more than a mention that &
is also available does little to impress me. I had actually welcomed &
when I found it in VS 2003 but my constantly trying to use it in a C#
project recently frustrated me greatly so I might be heading back to +
even in VB so my brain has one less thing to remember as I move back and
forth between languages.
James Hahn wrote:
>My "Language Reference", which I presume is VB 1 since it doesn't have a
version number, does not include the & as a string concatenation
operator, but it was there in Version 3 (1993) and after, so it's been
around for a very long time. That's the reason for the suggestion that
most VB programmers would consider the use of + as indicating the
programmer is a recent starter with VB who has brought coding habits from
another language.

"cj2" <cj*@nospam.nospamwrote in message
news:uB**************@TK2MSFTNGP03.phx.gbl...
>>>

snip >
I Never said every language uses +. I said "every language I have used
in the past". If VB up to VB4 had & I didn't know about it. I jumped
from VB4 to VB2003 so if VB5 or 6 added & I wouldn't know. In the past
I've also used COBOL, C, C++, dBase, Clipper and Pascal and don't
remember any of them using &.
Sep 13 '08 #14

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

Similar topics

2
by: ezelasky | last post by:
We are using the bcp utility (via APIs) to export data from a SQL table in a fixed format text file. BCP is inserting spaces for a field if the field contains a NULL. This is fine with us except...
6
by: WindAndWaves | last post by:
Hi Gurus In my quest in putting my first javascript together, I am now trying to conquer something that seems trivial, but has taken me hours. I would like to format a field in a form once the...
2
by: Dalan | last post by:
This should not be an issue, but it is. I'm sure that someone knows what little piece of code is needed too persuade Access 97 to include a currency format for labels (Avery, mailing type). Have...
4
by: intl04 | last post by:
I have a memo field that is included in some Access reports I created. Is there some way for the memo field to display nicely formatted text, with line breaks between paragraphs? Or is it necessary...
1
by: Julius Fenata | last post by:
Dear all, I need help to change my item-template value format... Here is my case, I have a datagrid, with 'SubjectPrice' field, and when the grid displayed, my 'SubjectPrice' field displayed...
6
by: Ted | last post by:
I used bcp to produce the apended format file. How can it be modified to recognize the quotes that surround the text fields and not insert the quotes along with the text? Invariably, the first...
9
by: RMC | last post by:
Hello, I'm looking for a way to parse/format a memo field within a report. The Access 2000 database (application) has an equipment table that holds a memo field. Within the report, the memo...
3
by: veaux | last post by:
The date format mmddyyyy is common but I realize it's not a standard format in Access. There must be a simple way to provide this format to Access so it treats it as a date? In past, I play with...
3
by: Bface | last post by:
Hi all, Hope everyone had a good holiday. I am having a difficult time changing the date format of a field from Excel. I have never had this problem before. I link the excel spreadsheet to my DB,...
5
by: The Pipe | last post by:
Hello I have a table that I need to export to a .asc file format. I have had several attempts at getting this to work but with no luck. Basically the file would need to have every record...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.