Convert string to Numeric in VB.net 2003 ?

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

    Convert string to Numeric in VB.net 2003 ?

    How do I convert string to numeric in VB.NET 2003 ?

    Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
    then not convert.

    Regards,

    Tee


  • kimiraikkonen

    #2
    Re: Convert string to Numeric in VB.net 2003 ?

    On Sep 29, 11:03 am, "engteng" <pass...@gmail. comwrote:
    How do I convert string to numeric in VB.NET 2003 ?
    >
    Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
    then not convert.
    >
    Regards,
    >
    Tee
    In my idea, first you should remove P by taking only numeric part
    using substring function, then you're ready to cast it to Integer or
    Long using CInt or CLng depending on the range as follows:

    ' For P50001
    Dim num1 As String = "P50001"
    num1 = num1.Substring( 1)
    'Proove that 50001 is now an Int32
    MsgBox(num1 & " is " & _
    CInt(num1).GetT ype.Name.ToStri ng)

    ' For 50001P
    Dim num2 As String = "50001P"
    num2 = num2.Substring( 0, 5)
    MsgBox(num2 & " is " & _
    CInt(num2).GetT ype.Name.ToStri ng)

    '.........

    Hope this helps,

    Onur Güzel

    Comment

    • =?ISO-8859-1?Q?Lorenz_H=F6lscher?=

      #3
      Re: Convert string to Numeric in VB.net 2003 ?

      Hello Tee,

      if the "P" always is the single first or last character you could
      write something like:

      _______________ _______________ _____________
      dim strX as string
      dim dblValue as Short

      strX="P50001"
      if LCase(Left(strX ,1))="p" then
      dblvalue = val(mid(strX,2) )
      elseif LCase(Right(str X,1))="p" then
      dblvalue = val(left(strX,L en(strX)-1))
      else
      dblvalue = 0
      end if
      _______________ _______________ _____________

      If the character may vary you could change the code into something
      like:

      _______________ _______________ _____________
      if Not IsNumeric(LCase (Left(strX,1))) then
      _______________ _______________ _____________


      (I didn't check it with the debugger so I hope it's free of errors)

      Good luck,
      Lorenz

      Comment

      • Bill McCarthy

        #4
        Re: Convert string to Numeric in VB.net 2003 ?

        Hi Lorenz,

        Generally it is better to use a string comparison method than convert
        strings to lower case or upper case for the purpose of a case insensitive
        match, eg:

        If s.StartsWith("P ", StringCompariso n.CurrentCultur eIgnoreCase) Then
        s = s.Substring(1)
        ElseIf s.EndsWith("P", StringCompariso n.CurrentCultur eIgnoreCase) Then
        s = s.Substring(0, s.Length - 1)
        End If

        value = CInt(s)




        "Lorenz Hölscher" <internet@softw are-dozent.dewrote in message
        news:e49e4ade-fbb1-4a2f-a86c-26805982a438@p2 5g2000hsf.googl egroups.com...
        Hello Tee,
        >
        if the "P" always is the single first or last character you could
        write something like:
        >
        _______________ _______________ _____________
        dim strX as string
        dim dblValue as Short
        >
        strX="P50001"
        if LCase(Left(strX ,1))="p" then
        dblvalue = val(mid(strX,2) )
        elseif LCase(Right(str X,1))="p" then
        dblvalue = val(left(strX,L en(strX)-1))
        else
        dblvalue = 0
        end if
        _______________ _______________ _____________
        >
        If the character may vary you could change the code into something
        like:
        >
        _______________ _______________ _____________
        if Not IsNumeric(LCase (Left(strX,1))) then
        _______________ _______________ _____________
        >
        >
        (I didn't check it with the debugger so I hope it's free of errors)
        >
        Good luck,
        Lorenz

        Comment

        • Bill McCarthy

          #5
          Re: Convert string to Numeric in VB.net 2003 ?

          Hi Tee,

          You can try:

          value = CInt( s.Trim("P"c, "p"c))


          "engteng" <passrcv@gmail. comwrote in message
          news:eHO2eogIJH A.3932@TK2MSFTN GP03.phx.gbl...
          How do I convert string to numeric in VB.NET 2003 ?
          >
          Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
          then not convert.
          >
          Regards,
          >
          Tee
          >
          >

          Comment

          • =?ISO-8859-1?Q?Lorenz_H=F6lscher?=

            #6
            Re: Convert string to Numeric in VB.net 2003 ?

            Hi Bill,

            you're right for sure. As you might have seen I usually work with
            Office-VBA with no SubString-Method und its IgnoreCase-Parameter until
            now...

            bye, Lorenz

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Convert string to Numeric in VB.net 2003 ?

              Tee,

              dim Example as string = "P50001p".tolow er.Trim("p")

              Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current string are removed.


              Cor

              "engteng" <passrcv@gmail. comschreef in bericht
              news:eHO2eogIJH A.3932@TK2MSFTN GP03.phx.gbl...
              How do I convert string to numeric in VB.NET 2003 ?
              >
              Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
              then not convert.
              >
              Regards,
              >
              Tee
              >
              >

              Comment

              • kimiraikkonen

                #8
                Re: Convert string to Numeric in VB.net 2003 ?

                On Sep 29, 2:06 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                wrote:
                Tee,
                >
                dim Example as string = "P50001p".tolow er.Trim("p")
                >
                Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current string are removed.

                >
                Cor
                >
                "engteng" <pass...@gmail. comschreef in berichtnews:eHO 2eogIJHA.3932@T K2MSFTNGP03.phx .gbl...
                >
                >
                >
                How do I convert string to numeric in VB.NET 2003 ?
                >
                Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
                then not convert.
                >
                Regards,
                >
                Tee- Hide quoted text -
                >
                - Show quoted text -
                Why are you trying to lower P? OP just wants to convert "P50001 to
                50001" or "50001P to 50001". Plus, trimming 'P' is not enough alone,
                also the string type must be converted to a numeric type such as
                Integer for further usage. In this case, it's proper to take the part
                of the string value that can be casted to Integer using CInt or
                Convert.ToInt32 etc. (without 'P').

                Thanks,

                Onur G.

                Comment

                • Cor Ligthert[MVP]

                  #9
                  Re: Convert string to Numeric in VB.net 2003 ?

                  Onur,

                  The first I agree, it is converting the P in the middle as well, to lower.

                  But how can you convert a string with a P in the middle to a what you call a
                  numeric?

                  That can only by those who call a string with all numeric characters a
                  numeric, and therefore I did not extend the code for that part.

                  (By the way, I have tried the code, with that what you did, you can do the
                  same, as you then set a simple Cint before it, you can use it as any real
                  numeric value instead of a string, however this can in my idea never been
                  done in the way the Op was asking).

                  Cor

                  "kimiraikko nen" <kimiraikkonen8 5@gmail.comschr eef in bericht
                  news:102d99d7-a5f1-444f-b863-6e77d39c5c12@x4 1g2000hsb.googl egroups.com...
                  On Sep 29, 2:06 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                  wrote:
                  Tee,
                  >
                  dim Example as string = "P50001p".tolow er.Trim("p")
                  >
                  Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current string are removed.

                  >
                  Cor
                  >
                  "engteng" <pass...@gmail. comschreef in
                  berichtnews:eHO 2eogIJHA.3932@T K2MSFTNGP03.phx .gbl...
                  >
                  >
                  >
                  How do I convert string to numeric in VB.NET 2003 ?
                  >
                  Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
                  then not convert.
                  >
                  Regards,
                  >
                  Tee- Hide quoted text -
                  >
                  - Show quoted text -
                  Why are you trying to lower P? OP just wants to convert "P50001 to
                  50001" or "50001P to 50001". Plus, trimming 'P' is not enough alone,
                  also the string type must be converted to a numeric type such as
                  Integer for further usage. In this case, it's proper to take the part
                  of the string value that can be casted to Integer using CInt or
                  Convert.ToInt32 etc. (without 'P').

                  Thanks,

                  Onur G.

                  Comment

                  • Kevin S Gallagher

                    #10
                    Re: Convert string to Numeric in VB.net 2003 ?

                    How about using regular expressions
                    Dim SomeValue As String = "P500P01"
                    Dim SomeNumber As Integer = 0
                    Dim objRegEx As New Regex("[p]")
                    SomeNumber = CInt(objRegEx.R eplace(SomeValu e.ToLower, ""))

                    "engteng" <passrcv@gmail. comwrote in message
                    news:eHO2eogIJH A.3932@TK2MSFTN GP03.phx.gbl...
                    How do I convert string to numeric in VB.NET 2003 ?
                    >
                    Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle
                    then not convert.
                    >
                    Regards,
                    >
                    Tee
                    >
                    >

                    Comment

                    Working...