Format a text field

  • Thread starter Thread starter cj2
  • Start date Start date
C

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.Substring(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.Substring(3,3)+"-"+thisCustNo.Substring(6,4)

Am I missing something?
 
Use whichever seems easier to you. Your second example is correct except for
the use of + instead of &. You could also examine regular expressions.
 
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
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
 
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.
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]" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
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 said:
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.
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]" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Cor said:
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 said:
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.
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]"" <[email protected]> schreef in bericht
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
CJ2 wrote:
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.
 
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.
 
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.
 
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.
 
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.
 
Back
Top