Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Get a string of number ?

Get a string of number ?

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dec82
    wrote on last edited by
    #1

    i have a string abcd 36.2 How to get the value 36.2 only , with abcd can be changed?

    A L J F 4 Replies Last reply
    0
    • D dec82

      i have a string abcd 36.2 How to get the value 36.2 only , with abcd can be changed?

      A Offline
      A Offline
      Anoop Brijmohun
      wrote on last edited by
      #2

      Hi one option is that you can loop through the entire string and check for numerics ie.

      Dim x As String = "abcd 36.2"
      Dim value As String = ""
      For lp As Integer = 0 To x.Length - 1
      If IsNumeric(x.Chars(lp)) Or x.Chars(lp) = "." Then
      value &= x.Chars(lp)
      End If
      Application.DoEvents()
      Next
      MsgBox(value)

      hope this helps Anoop

      D D 2 Replies Last reply
      0
      • D dec82

        i have a string abcd 36.2 How to get the value 36.2 only , with abcd can be changed?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        There's always a space separating the text and the number? You could split it up on the space;

        Dim MyString() As String = "Test 123".Split(' ');

        You can get the integer-part like this;

        Dim MyNum As Integer = Int32.Parse(MyString(1))

        I are troll :)

        1 Reply Last reply
        0
        • A Anoop Brijmohun

          Hi one option is that you can loop through the entire string and check for numerics ie.

          Dim x As String = "abcd 36.2"
          Dim value As String = ""
          For lp As Integer = 0 To x.Length - 1
          If IsNumeric(x.Chars(lp)) Or x.Chars(lp) = "." Then
          value &= x.Chars(lp)
          End If
          Application.DoEvents()
          Next
          MsgBox(value)

          hope this helps Anoop

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          That's the hard, slow, and non-globalized way to do it...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          1 Reply Last reply
          0
          • A Anoop Brijmohun

            Hi one option is that you can loop through the entire string and check for numerics ie.

            Dim x As String = "abcd 36.2"
            Dim value As String = ""
            For lp As Integer = 0 To x.Length - 1
            If IsNumeric(x.Chars(lp)) Or x.Chars(lp) = "." Then
            value &= x.Chars(lp)
            End If
            Application.DoEvents()
            Next
            MsgBox(value)

            hope this helps Anoop

            D Offline
            D Offline
            dec82
            wrote on last edited by
            #5

            thanks i have run time error 424 Object required .How do i solve it ? Thanks\

            1 Reply Last reply
            0
            • D dec82

              i have a string abcd 36.2 How to get the value 36.2 only , with abcd can be changed?

              J Offline
              J Offline
              Jon_Boy
              wrote on last edited by
              #6

              Is the format always going to be 'AAAA ##.##'? Meaning is the value always going to be 4 letters followed by a space?

              Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

              D 1 Reply Last reply
              0
              • D dec82

                i have a string abcd 36.2 How to get the value 36.2 only , with abcd can be changed?

                F Offline
                F Offline
                Fernando Soto
                wrote on last edited by
                #7

                Hi dec82; you could use a regular expression to pull the numeric data out.

                    ' Test data
                    Dim testData As String = "abcd 36.2"
                    ' String to hold the wanted info
                    Dim numericValue As String = String.Empty
                    ' A regular Expression to pull out the needed info
                    Dim m As Match = Regex.Match(testData, "^.\*?(\\d+(?:\\.\\d+)?)")
                    ' Test to see if the string had a numeric value on the end of it
                    If m.Success Then
                        ' Pull out the information
                        numericValue = m.Groups(1).Value
                    End If
                
                    MessageBox.Show("Numeric Value = " & numericValue)
                

                Fernando

                1 Reply Last reply
                0
                • J Jon_Boy

                  Is the format always going to be 'AAAA ##.##'? Meaning is the value always going to be 4 letters followed by a space?

                  Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

                  D Offline
                  D Offline
                  dec82
                  wrote on last edited by
                  #8

                  No, lenghth of the string in front are variable . My purpose to get the number inside?

                  L 1 Reply Last reply
                  0
                  • D dec82

                    No, lenghth of the string in front are variable . My purpose to get the number inside?

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, split the problem in a couple of smaller ones. There are at least two parts: 1. locate the "word" that contains the number 2. translate that word to a numeric value 1. depends on what you do know about the pattern in the string. e.g. if the number is always at the end, find the last space, get the substring behind it. if there is exactly one word plus a space in front, find that first space and look for a possible space after the number too. 2. is easy with int.Parse or int.TryParse If you were really lazy (I wouldn't dare to assume that), split the entire string in words based on a space, then foreach "word" do a TryParse until one succeeds. And now you are on your own I guess. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups