Results 1 to 7 of 7

Thread: How to extract numbers from a series of characters and then sum them

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2018
    Posts
    21

    How to extract numbers from a series of characters and then sum them

    So, basically my idea is that I have one button and one text box. The user inputs chars+numbers in the text box and when he clicks on the button the numbers withing the string will be summed.

    EXAMPLE: d34ghjki65regc8cbrs42svtn7acz8

    I am not sure what to do here since I failed multiple times so far..
    Still a newbie in VB, so I'm still learning and understanding.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to extract numbers from a series of characters and then sum them

    The simplest option is to use a LINQ query but that's probably not the way to go for someone at your level of understanding. As such, you should use a loop. One Important thing to note is that a String is an enumerable list of Char values, which means that you can use a For Each loop to access each character in turn. Another point is that the Char.IsDigit method can tell you whether a Char represents a numeric digit. That should be all you need to know. The rest is simple logic.

    If I just gave you a piece of paper with those characters on it then I've no doubt that you could perform this task. If you can do that then you already know what the steps are that you need to perform. Put some thought into that. What are those steps? Formalise them and write them down. Once you have done that, you can write code to implement the steps, one at a time. If you still can't get the right result, at least you can post your algorithm and the code you've written and explain exactly how the code doesn't do what you expect.

    You also need to be able to debug that code properly, i.e. set a breakpoint and step through the code line by line. If you don't already know how to do that, now would be the time to learn.

  3. #3
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: How to extract numbers from a series of characters and then sum them

    you can find some examples with a google search "vb.net extract numbers from string"
    Last edited by patel45; Aug 14th, 2018 at 03:37 AM.

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: How to extract numbers from a series of characters and then sum them

    I wanted to take a knock at it myself because I wasn't familiar with Char.IsDigit.

    Code:
    Dim mString As String = "d34ghjki65regc8cbrs42svtn7acz8"
    Dim mResult As Integer = 0
    
    For Each c As Char In mString
        If Char.IsDigit(c) Then
    	 mResult = mResult + Integer.Parse(c)
        End If
    Next
    
    MessageBox.Show(mResult)
    Please remember next time...elections matter!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2018
    Posts
    21

    Re: How to extract numbers from a series of characters and then sum them

    Quote Originally Posted by TysonLPrice View Post
    I wanted to take a knock at it myself because I wasn't familiar with Char.IsDigit.

    Code:
    Dim mString As String = "d34ghjki65regc8cbrs42svtn7acz8"
    Dim mResult As Integer = 0
    
    For Each c As Char In mString
        If Char.IsDigit(c) Then
    	 mResult = mResult + Integer.Parse(c)
        End If
    Next
    
    MessageBox.Show(mResult)
    Quote Originally Posted by jmcilhinney View Post
    The simplest option is to use a LINQ query but that's probably not the way to go for someone at your level of understanding. As such, you should use a loop. One Important thing to note is that a String is an enumerable list of Char values, which means that you can use a For Each loop to access each character in turn. Another point is that the Char.IsDigit method can tell you whether a Char represents a numeric digit. That should be all you need to know. The rest is simple logic.

    If I just gave you a piece of paper with those characters on it then I've no doubt that you could perform this task. If you can do that then you already know what the steps are that you need to perform. Put some thought into that. What are those steps? Formalise them and write them down. Once you have done that, you can write code to implement the steps, one at a time. If you still can't get the right result, at least you can post your algorithm and the code you've written and explain exactly how the code doesn't do what you expect.

    You also need to be able to debug that code properly, i.e. set a breakpoint and step through the code line by line. If you don't already know how to do that, now would be the time to learn.
    I eventually cracked my head with it, but now I got one more question; let's say, for example that i would like to extract numbers as double digits if such, of course, exist (e.g.: "d34ghjki65regc8cbrs42svtn7acz8"). How do I make that happen?

    I'll use Tyson's code for an example of code:

    Code:
    Dim mString As String = "d34ghjki65regc8cbrs42svtn7acz8"
    Dim mResult As Integer = 0
    
    For Each c As Char In mString
        If Char.IsDigit(c) Then
    	 mResult = mResult + Integer.Parse(c)
        End If
    Next
    
    MessageBox.Show(mResult)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to extract numbers from a series of characters and then sum them

    Quote Originally Posted by agent_zero View Post
    How do I make that happen?
    How do you think you would do it? Maybe give it a try and then post back if and when you actually encounter an issue.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to extract numbers from a series of characters and then sum them

    Try this...

    Code:
    Dim mString As String = "d34ghjki65regc8cbrs42svtn7acz8"
    Dim mResult As Integer = 0
    Dim number As String = "0"
    
    For Each c As Char In mString
        If Char.IsDigit(c) Then
            number &= c
        Else
            mResult = mResult + Integer.Parse(number)
            number = "0"
        End If
    Next
    If number <> "0" Then
        mResult = mResult + Integer.Parse(number)
    End If
    
    MessageBox.Show(mResult.ToString)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width