-
Aug 14th, 2018, 02:27 AM
#1
Thread Starter
Junior Member
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.
-
Aug 14th, 2018, 02:34 AM
#2
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.
-
Aug 14th, 2018, 03:20 AM
#3
Addicted Member
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.
-
Aug 14th, 2018, 05:32 AM
#4
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!
-
Aug 14th, 2018, 06:06 AM
#5
Thread Starter
Junior Member
Re: How to extract numbers from a series of characters and then sum them
Originally Posted by TysonLPrice
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)
Originally Posted by jmcilhinney
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)
-
Aug 14th, 2018, 08:44 AM
#6
Re: How to extract numbers from a series of characters and then sum them
Originally Posted by agent_zero
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.
-
Aug 16th, 2018, 07:12 PM
#7
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|