Format a text field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cj2

    Format a text field

    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.Form at("({0}){1}-{2}",thisCustNo .Substring(0,3) ,thisCustNo.Sub string(3,3),thi sCustNo.Substri ng(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.Su bstring(6,4)

    Am I missing something?
  • James Hahn

    #2
    Re: Format a text field

    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" <cj2@nospam.nos pamwrote in message
    news:%23wmobJ4E JHA.3408@TK2MSF TNGP04.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.Form at("({0}){1}-{2}",thisCustNo .Substring(0,3) ,thisCustNo.Sub string(3,3),thi sCustNo.Substri ng(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.Su bstring(6,4)
    >
    Am I missing something?

    Comment

    • Jeffrey Tan[MSFT]

      #3
      RE: Format a text field

      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:
      msdnmg@microsof t.com.

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

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Format a text field

        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]"" <jetan@online.m icrosoft.comsch reef in bericht
        news:mlJxZq7EJH A.4532@TK2MSFTN GHUB02.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:
        msdnmg@microsof t.com.
        >
        This posting is provided "AS IS" with no warranties, and confers no
        rights.
        >

        Comment

        • cj2

          #5
          Re: Format a text field

          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]"" <jetan@online.m icrosoft.comsch reef in bericht
          news:mlJxZq7EJH A.4532@TK2MSFTN GHUB02.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
          >concatenatio n. "+" 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:
          >msdnmg@microsof t.com.
          >>
          >This posting is provided "AS IS" with no warranties, and confers no
          >rights.
          >>
          >

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: Format a text field

            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" <cj2@nospam.nos pamschreef in bericht
            news:uXonvYAFJH A.5104@TK2MSFTN GP02.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]"" <jetan@online.m icrosoft.comsch reef in bericht
            >news:mlJxZq7EJ HA.4532@TK2MSFT NGHUB02.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:
            >>msdnmg@microsof t.com.
            >>>
            >>This posting is provided "AS IS" with no warranties, and confers no
            >>rights.
            >>>
            >>

            Comment

            • cj2

              #7
              Re: Format a text field



              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" <cj2@nospam.nos pamschreef in bericht
              news:uXonvYAFJH A.5104@TK2MSFTN GP02.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]"" <jetan@online.m icrosoft.comsch reef in bericht
              >>news:mlJxZq7E JHA.4532@TK2MSF TNGHUB02.phx.gb l...
              >>>Hi CJ,
              >>>>
              >>>Both solutions should work correctly. However, just as James pointed
              >>>out,
              >>>in the second solution, we'd better use "&" operator for string
              >>>concatenatio n. "+" 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
              >>>suggestion s 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:
              >>>msdnmg@microsof t.com.
              >>>>
              >>>This posting is provided "AS IS" with no warranties, and confers no
              >>>rights.
              >>>>
              >>>
              >

              Comment

              • Branco Medeiros

                #8
                Re: Format a text field

                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.

                Comment

                • cj2

                  #9
                  Re: Format a text field

                  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.

                  Comment

                  • James Hahn

                    #10
                    Re: Format a text field

                    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" <cj2@nospam.nos pamwrote in message
                    news:uBqwgvDFJH A.5484@TK2MSFTN GP03.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 &.
                    >

                    Comment

                    • cj2

                      #11
                      Re: Format a text field

                      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" <cj2@nospam.nos pamwrote in message
                      news:uBqwgvDFJH A.5484@TK2MSFTN GP03.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 &.
                      >>
                      >

                      Comment

                      • Jack Jackson

                        #12
                        Re: Format a text field

                        On Fri, 12 Sep 2008 10:25:35 -0400, cj2 <cj2@nospam.nos pamwrote:

                        >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.

                        Comment

                        • James Hahn

                          #13
                          Re: Format a text field

                          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" <cj2@nospam.nos pamwrote in message
                          news:e7NlsNOFJH A.5060@TK2MSFTN GP03.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" <cj2@nospam.nos pamwrote in message
                          >news:uBqwgvDFJ HA.5484@TK2MSFT NGP03.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 &.
                          >>>
                          >>

                          Comment

                          • Cor Ligthert[MVP]

                            #14
                            Re: Format a text field

                            Such a long message, what is in fact a farm of dead horses in this
                            newsgroup)

                            :-)

                            Cor


                            "cj2" <cj2@nospam.nos pamschreef in bericht
                            news:e7NlsNOFJH A.5060@TK2MSFTN GP03.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" <cj2@nospam.nos pamwrote in message
                            >news:uBqwgvDFJ HA.5484@TK2MSFT NGP03.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 &.
                            >>>
                            >>

                            Comment

                            Working...