string work

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

    #1

    string work

    i'm trying to change a number inside a string. (i.e. 5.39) if the
    number in position 2 is < = 4 then change it to 4 if its = 5
    change it to a 9.

    how would i do this in vb 2005?

    thanks.

  • Cor Ligthert[MVP]

    #2
    Re: string work

    jdrott,

    You can use the If and the Mid command for this.

    As you are familiar with VB6 it is the same as that.

    Cor

    "jdrott1" <jonathandrott@ gmail.comschree f in bericht
    news:1185828227 .679188.79000@1 9g2000hsx.googl egroups.com...
    i'm trying to change a number inside a string. (i.e. 5.39) if the
    number in position 2 is < = 4 then change it to 4 if its = 5
    change it to a 9.
    >
    how would i do this in vb 2005?
    >
    thanks.
    >

    Comment

    • Jack Jackson

      #3
      Re: string work

      On Mon, 30 Jul 2007 20:43:47 -0000, jdrott1 <jonathandrott@ gmail.com>
      wrote:
      >i'm trying to change a number inside a string. (i.e. 5.39) if the
      >number in position 2 is < = 4 then change it to 4 if its = 5
      >change it to a 9.
      >
      >how would i do this in vb 2005?
      I doubt you really want to do what you say. I'm not sure which
      character you think is in position 2, so I assumed it is the '3'. You
      probably don't want to assume that the decimal point is the second
      character, use _str.IndexOf(". "c) to find out where it is. You
      probably also don't want the second Substring call in the last line
      which keeps the digits beyond the one that is changed.

      Dim _chr As String
      Dim _index As Integer = 2
      Dim _str As String = "5.39"

      If _str.Substring( _index, 1).ToInt32 <= 4
      _chr = "4"
      Else
      _chr = "9"
      End If
      _str = _str.Substring( 0, _index) + _chr + _str.SubString( _index + 1)

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: string work

        jdrott1 wrote:
        i'm trying to change a number inside a string. (i.e. 5.39) if the
        number in position 2 is < = 4 then change it to 4 if its = 5
        change it to a 9.
        Why do you have the number in a string? As what you want is a numeric
        operation, convert it to a number.

        Dim price as Double
        price = Double.Parse(th eString)
        price += 0.01
        price *= 2
        price = Math.Floor(pric e)
        price /= 2
        price -= 0.01

        Use price.ToString( "0.00") to create a string in the same format as the
        original.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: string work

          Göran Andersson wrote:
          jdrott1 wrote:
          >i'm trying to change a number inside a string. (i.e. 5.39) if the
          >number in position 2 is < = 4 then change it to 4 if its = 5
          >change it to a 9.
          >
          Why do you have the number in a string? As what you want is a numeric
          operation, convert it to a number.
          >
          Dim price as Double
          price = Double.Parse(th eString)
          price += 0.01
          price *= 2
          price = Math.Floor(pric e)
          That should of course be Math.Ceiling.
          price /= 2
          price -= 0.01
          >
          Use price.ToString( "0.00") to create a string in the same format as the
          original.
          >

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...