Substring question

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

    #1

    Substring question

    In VB 6 I used to do like the following:
    if left(str,3) = "abc" then

    In VB.NET when I do the following
    sStr.Substring( 0, 3), and the sStr has fewer than 3 characters, it will give
    me an error.

    For ex: sStr = "a", if I do sStr.Substring( 0, 3), it will give me an error.
    So, I was forced to do

    if len(sStr) >= 3 then
    if sStr.Substring( 0, 3)
    :
    Is there an easier way to to it besides the above code ?

    Thanks


  • Marina Levit [MVP]

    #2
    Re: Substring question

    In this particular case, I would use:

    If str.StartsWith( "abc") Then
    ....

    "fniles" <fniles@pfmail. com> wrote in message
    news:ezjKGxfbGH A.3840@TK2MSFTN GP04.phx.gbl...[color=blue]
    > In VB 6 I used to do like the following:
    > if left(str,3) = "abc" then
    >
    > In VB.NET when I do the following
    > sStr.Substring( 0, 3), and the sStr has fewer than 3 characters, it will
    > give me an error.
    >
    > For ex: sStr = "a", if I do sStr.Substring( 0, 3), it will give me an
    > error. So, I was forced to do
    >
    > if len(sStr) >= 3 then
    > if sStr.Substring( 0, 3)
    > :
    > Is there an easier way to to it besides the above code ?
    >
    > Thanks
    >
    >[/color]


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Substring question

      Fniles,

      Why do you not use that same instructions as in VB6 as they are there if you
      are used to those. They are legal (as well in the future) in VBNet.

      The most of the VB functions have something extra, like to overcome the
      problem as you wrote.

      Cor


      "fniles" <fniles@pfmail. com> schreef in bericht
      news:ezjKGxfbGH A.3840@TK2MSFTN GP04.phx.gbl...[color=blue]
      > In VB 6 I used to do like the following:
      > if left(str,3) = "abc" then
      >
      > In VB.NET when I do the following
      > sStr.Substring( 0, 3), and the sStr has fewer than 3 characters, it will
      > give me an error.
      >
      > For ex: sStr = "a", if I do sStr.Substring( 0, 3), it will give me an
      > error. So, I was forced to do
      >
      > if len(sStr) >= 3 then
      > if sStr.Substring( 0, 3)
      > :
      > Is there an easier way to to it besides the above code ?
      >
      > Thanks
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Substring question

        "Marina Levit [MVP]" <someone@nospam .com> schrieb:[color=blue]
        > In this particular case, I would use:
        >
        > If str.StartsWith( "abc") Then[/color]

        Note that is is not 100 % equivalent:

        \\\
        MsgBox("Foo".St artsWith(ChrW(& HFEFF) & "Foo")
        ///

        will return 'True' on a 'de-DE' and 'en-US' system in .NET 2.0, for example.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Oenone

          #5
          Re: Substring question

          fniles wrote:[color=blue]
          > Is there an easier way to to it besides the above code ?[/color]

          Yep, just keep using the Left() function.

          There are lots of VB6-style string functions that I prefer to use over the
          methods performed on strings themselves. One reason for this is that if you
          string is uninitialised, you'll get a Null Reference exception if you try to
          call a method on it. The VB6 string commands handle this without any
          problem. This VB6-style code:

          \\\
          If Len(myString) > 0 Then
          [...]
          End If
          ///

          ....would otherwise turn into the much less readable:

          \\\
          If myString IsNot Nothing AndAlso myString.Length > 0 Then
          [...]
          End If
          ///

          The first piece of code is much more concise.

          --

          (O)enone


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Substring question

            Oenone,
            A .NET 2.0 alternative to:
            | \\\
            | If myString IsNot Nothing AndAlso myString.Length > 0 Then
            | [...]
            | End If
            | ///

            Would be String.IsNullOr Empty

            If String.IsNullOr Empty(myString) Then
            [...]
            End If

            http://msdn2.microsoft.com/en-us/lib...3e(vs.80).aspx

            I agree, your first is more concise; I find IsNullOrEmpty to be more
            "obvious", while "myString IsNot Nothing AndAlso myString.Length > 0" or
            "myString IsNot Nothing AndAlso myString <> "" " are overly wordy.

            By obvious I mean the code states more clearly what its doing. Its checking
            the string variable to see if its null or empty. The Len function simply
            says I am requesting the length of the parameter; I would use the Len or
            String.Length property when I needed to know what the length actually was...
            FWIW: I rarely use the length to check for empty, although I'm not opposed
            to using length to check for empty.

            --
            Hope this helps
            Jay B. Harlow [MVP - Outlook]
            ..NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net


            "Oenone" <oenone@nowhere .com> wrote in message
            news:mYM5g.1069 $LU3.693@newsfe 2-gui.ntli.net...
            | fniles wrote:
            | > Is there an easier way to to it besides the above code ?
            |
            | Yep, just keep using the Left() function.
            |
            | There are lots of VB6-style string functions that I prefer to use over the
            | methods performed on strings themselves. One reason for this is that if
            you
            | string is uninitialised, you'll get a Null Reference exception if you try
            to
            | call a method on it. The VB6 string commands handle this without any
            | problem. This VB6-style code:
            |
            | \\\
            | If Len(myString) > 0 Then
            | [...]
            | End If
            | ///
            |
            | ...would otherwise turn into the much less readable:
            |
            | \\\
            | If myString IsNot Nothing AndAlso myString.Length > 0 Then
            | [...]
            | End If
            | ///
            |
            | The first piece of code is much more concise.
            |
            | --
            |
            | (O)enone
            |
            |


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Substring question

              Fniles,
              As the others suggest you can continue to use the Left function in .NET.

              One caveat, the Control.Left hides VB's Left function (in a form for
              example), normally I introduce a namespace alias & use VB.Left when I
              need/want to use the function.

              Something
              Imports VB = Microsoft.Visua lBasic

              If VB.Left(str,3) = "abc" Then

              --
              Hope this helps
              Jay B. Harlow [MVP - Outlook]
              ..NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "fniles" <fniles@pfmail. com> wrote in message
              news:ezjKGxfbGH A.3840@TK2MSFTN GP04.phx.gbl...
              | In VB 6 I used to do like the following:
              | if left(str,3) = "abc" then
              |
              | In VB.NET when I do the following
              | sStr.Substring( 0, 3), and the sStr has fewer than 3 characters, it will
              give
              | me an error.
              |
              | For ex: sStr = "a", if I do sStr.Substring( 0, 3), it will give me an
              error.
              | So, I was forced to do
              |
              | if len(sStr) >= 3 then
              | if sStr.Substring( 0, 3)
              | :
              | Is there an easier way to to it besides the above code ?
              |
              | Thanks
              |
              |


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: Substring question

                Jay,
                What is character ChrW(&HFEFF) anyway?

                You could always use:

                MsgBox("Foo".St artsWith(ChrW(& HFEFF) & "Foo",
                StringCompariso n.OrdinalIgnore Case))

                FWIW: I tried StringCompariso n.InvariantCult ure above & it also returns
                true.

                In VB6 did you try Option Compare Text or Option Compare Binary?

                Depending on what ChrW(&HFEFF) is, the results you see may be the more
                correct (between VB6 & .NET).

                --
                Hope this helps
                Jay B. Harlow [MVP - Outlook]
                ..NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net


                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                news:OMu$lHgbGH A.4900@TK2MSFTN GP02.phx.gbl...
                | "Marina Levit [MVP]" <someone@nospam .com> schrieb:
                | > In this particular case, I would use:
                | >
                | > If str.StartsWith( "abc") Then
                |
                | Note that is is not 100 % equivalent:
                |
                | \\\
                | MsgBox("Foo".St artsWith(ChrW(& HFEFF) & "Foo")
                | ///
                |
                | will return 'True' on a 'de-DE' and 'en-US' system in .NET 2.0, for
                example.
                |
                | --
                | M S Herfried K. Wagner
                | M V P <URL:http://dotnet.mvps.org/>
                | V B <URL:http://classicvb.org/petition/>
                |


                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: Substring question

                  "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> schrieb:[color=blue]
                  > What is character ChrW(&HFEFF) anyway?[/color]

                  U+FEFF "ZERO WIDTH NON-BREAKING SPACE"
                  U+2060 "WORD JOINER"

                  U+FEFF is "deprecated " and U+2060 should be used instead because U+FEFF is a
                  typical UTF-BOM too. However, the behavior is the same for the U+2060
                  character. The problem I am seeing is that the behavior changed from .NET
                  1.* to .NET 2.0.
                  [color=blue]
                  > You could always use:
                  >
                  > MsgBox("Foo".St artsWith(ChrW(& HFEFF) & "Foo",
                  > StringCompariso n.OrdinalIgnore Case))
                  >
                  > FWIW: I tried StringCompariso n.InvariantCult ure above & it also returns
                  > true.[/color]

                  Sure, I know! I have read a lot of code but I have rarely seen anybody
                  specifying this option, even if it should have been specified.
                  [color=blue]
                  > In VB6 did you try Option Compare Text or Option Compare Binary?[/color]

                  I tried it in VB.NET. 'Option Compare Text' performs a culture-specific
                  comparison while 'Option Compare Binary' performs an ordinal comparison.
                  With 'Option Compare Text' the expression '"Foo" = ChrW(&HFEFF) & "Foo"'
                  will return 'True', which isn't the case if 'Option Compare Binary' is used.
                  [color=blue]
                  > Depending on what ChrW(&HFEFF) is, the results you see may be the more
                  > correct (between VB6 & .NET).[/color]

                  I didn't compare VB6 to VB.NET in this particular case before posting. But
                  interestingly '"Foo" = ChrW(&HFEFF) & "Foo"' evaluates to 'True' with
                  'Option Compare Text' specified in VB6 while it evaluates to 'False' with
                  'Option Compare Binary'. So the behavior is not really new, but really
                  less-known.


                  --
                  M S Herfried K. Wagner
                  M V P <URL:http://dotnet.mvps.org/>
                  V B <URL:http://classicvb.org/petition/>

                  Comment

                  • Herfried K. Wagner [MVP]

                    #10
                    Re: Substring question

                    Jay,

                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> schrieb:[color=blue]
                    > A .NET 2.0 alternative to:
                    > | \\\
                    > | If myString IsNot Nothing AndAlso myString.Length > 0 Then
                    > | [...]
                    > | End If
                    > | ///
                    >
                    > Would be String.IsNullOr Empty[/color]

                    True, but don't forget to check out
                    <URL:http://msmvps.com/blogs/bill/archive/2006/04/04/89234.aspx>.

                    --
                    M S Herfried K. Wagner
                    M V P <URL:http://dotnet.mvps.org/>
                    V B <URL:http://classicvb.org/petition/>

                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Substring question

                      Herfried,
                      | U+FEFF is "deprecated " and U+2060 should be used instead because U+FEFF is
                      a
                      | typical UTF-BOM too.
                      I was going to say it looked like a BOM, odd it is (was) also a character.


                      | > FWIW: I tried StringCompariso n.InvariantCult ure above & it also returns
                      | > true.
                      |
                      | Sure, I know! I have read a lot of code but I have rarely seen anybody
                      | specifying this option, even if it should have been specified.
                      I try to use StringCompariso n.InvariantCult ure & StringCompariso n.Ordinal
                      where appropriate:

                      http://blogs.msdn.com/bclteam/archiv...01/424012.aspx
                      http://msdn.microsoft.com/netframewo...ngsinNET20.asp

                      Although sometimes I forget to use them initially, then remember on
                      subsequent code reviews... FWIW: I should thank you! Looking for where I
                      used the option I noticed a handful of places where I should have used
                      StringCompariso n.Ordinal instead of StringCompariso n.InvariantCult ure! I'll
                      need to change them sometime today...

                      --
                      Hope this helps
                      Jay B. Harlow [MVP - Outlook]
                      ..NET Application Architect, Enthusiast, & Evangelist
                      T.S. Bradley - http://www.tsbradley.net


                      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                      news:ufDe%23urb GHA.1204@TK2MSF TNGP02.phx.gbl. ..
                      | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> schrieb:
                      | > What is character ChrW(&HFEFF) anyway?
                      |
                      | U+FEFF "ZERO WIDTH NON-BREAKING SPACE"
                      | U+2060 "WORD JOINER"
                      |
                      | U+FEFF is "deprecated " and U+2060 should be used instead because U+FEFF is
                      a
                      | typical UTF-BOM too. However, the behavior is the same for the U+2060
                      | character. The problem I am seeing is that the behavior changed from .NET
                      | 1.* to .NET 2.0.
                      |
                      | > You could always use:
                      | >
                      | > MsgBox("Foo".St artsWith(ChrW(& HFEFF) & "Foo",
                      | > StringCompariso n.OrdinalIgnore Case))
                      | >
                      | > FWIW: I tried StringCompariso n.InvariantCult ure above & it also returns
                      | > true.
                      |
                      | Sure, I know! I have read a lot of code but I have rarely seen anybody
                      | specifying this option, even if it should have been specified.
                      |
                      | > In VB6 did you try Option Compare Text or Option Compare Binary?
                      |
                      | I tried it in VB.NET. 'Option Compare Text' performs a culture-specific
                      | comparison while 'Option Compare Binary' performs an ordinal comparison.
                      | With 'Option Compare Text' the expression '"Foo" = ChrW(&HFEFF) & "Foo"'
                      | will return 'True', which isn't the case if 'Option Compare Binary' is
                      used.
                      |
                      | > Depending on what ChrW(&HFEFF) is, the results you see may be the more
                      | > correct (between VB6 & .NET).
                      |
                      | I didn't compare VB6 to VB.NET in this particular case before posting.
                      But
                      | interestingly '"Foo" = ChrW(&HFEFF) & "Foo"' evaluates to 'True' with
                      | 'Option Compare Text' specified in VB6 while it evaluates to 'False' with
                      | 'Option Compare Binary'. So the behavior is not really new, but really
                      | less-known.
                      |
                      |
                      | --
                      | M S Herfried K. Wagner
                      | M V P <URL:http://dotnet.mvps.org/>
                      | V B <URL:http://classicvb.org/petition/>
                      |


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: Substring question

                        I would be very careful with statements that suggest you avoid
                        String.IsNullOr Empty specifically!

                        As I would find it *extremely* odd indeed if it (String.IsNullO rEmpty)
                        specifically caused a JIT bug to manifest. That *absolutely* *no* other
                        routine caused this bug to manifest.

                        Rather I would suspect any routine that is written similar to how
                        String.IsNullOr Empty is written when coupled with Bill's sample code would
                        cause the same JIT bug to manifest itself.

                        I would find more helpful & even expect statements that suggest you be
                        caution when using some particular code pattern... (the code pattern that
                        causes the JIT bug to manifest)...

                        Especially when every indication suggests it *is* a JIT bug (both Bill &
                        some of the comments suggest its a JIT bug). It sounds like a JIT bug to me.


                        Semantics yes, however I can see developers avoiding String.IsNullOr Empty
                        only to be smacked hard by the JIT bug!


                        Thanks for the heads up.

                        --
                        Hope this helps
                        Jay B. Harlow [MVP - Outlook]
                        ..NET Application Architect, Enthusiast, & Evangelist
                        T.S. Bradley - http://www.tsbradley.net


                        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                        news:OoyHAwrbGH A.4372@TK2MSFTN GP04.phx.gbl...
                        | Jay,
                        |
                        | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> schrieb:
                        | > A .NET 2.0 alternative to:
                        | > | \\\
                        | > | If myString IsNot Nothing AndAlso myString.Length > 0 Then
                        | > | [...]
                        | > | End If
                        | > | ///
                        | >
                        | > Would be String.IsNullOr Empty
                        |
                        | True, but don't forget to check out
                        | <URL:http://msmvps.com/blogs/bill/archive/2006/04/04/89234.aspx>.
                        |
                        | --
                        | M S Herfried K. Wagner
                        | M V P <URL:http://dotnet.mvps.org/>
                        | V B <URL:http://classicvb.org/petition/>
                        |


                        Comment

                        Working...