[RESOLVED] Convert dtring to an Int array?
This is an easy one I'm sure...and I'm also sure it involves a for loop and I'm kicking myself for not rememberign it.
Info: Using VB Studio 2008
need to convert a string consisting of numbers ie. "1234567890"
to a one dimensional int array where each caracter (digit) holds one place in the array.
Thanks in advance for the help.
Re: Convert dtring to an Int array?
Welcome to the forums.
You posted in wrong forum, .Net has its own. Notified moderators to move it for you.
Re: Convert dtring to an Int array?
Quote:
Originally Posted by
LaVolpe
Welcome to the forums.
You posted in wrong forum, .Net has its own. Notified moderators to move it for you.
That's a shame. It's a piece of cake in VB6. Why not let us solve OP's problem here?:sick:
Code:
Dim MyStr As String, MyNum(0 To 9) As Integer
MyStr = "1234567890"
For I = 1 To Len(MyStr)
MyNum(I - 1) = CInt(Mid$(MyStr, I, 1))
Next
Re: Convert dtring to an Int array?
47 views and no help?
Ok I know it should be something like this
txtNumber is the text box holding the string...and I need to get all the individual characters of the string into individual elements in a integer array....
The integer array is intNumberArray()
For i = 0 To txtNumber.text.length Step 1
(I have forgotten the commands to actually move data from the string to the array)
Next i
Re: Convert dtring to an Int array?
Quote:
Originally Posted by
Vedren
47 views and no help?
Ok I know it should be something like this
txtNumber is the text box holding the string...and I need to get all the individual characters of the string into individual elements in a integer array....
The integer array is intNumberArray()
For i = 0 To txtNumber.text.length Step 1
(I have forgotten the commands to actually move data from the string to the array)
Next i
See Post #3.
Re: Convert dtring to an Int array?
Yeah I don't think it is a .Net issue. I'm taking VBII and were using Visual Studio to create windows applications...
Re: Convert dtring to an Int array?
Thanks for the help but there is a problem with it we have not covered CInt or Mid$ or Len()we're still doing it the beginner (long) way, so while I could use that code I would be just copy pasting it in without actually understanding it which would defeat the purpose. For example were doing ....
For I = 1 To Len(MyStr)
like so
For i = 0 To txtNumber.text.length
Re: Convert dtring to an Int array?
Quote:
Originally Posted by
Vedren
Yeah I don't think it is a .Net issue. I'm taking VBII and were using Visual Studio to create windows applications...
I used to do this back in the QuickBasic days (please don't ask me the date nor my age at that point in time). :bigyello: ;)
Re: Convert dtring to an Int array?
Quote:
Originally Posted by
Vedren
Thanks for the help but there is a problem with it we have not covered CInt or Mid$ or Len()we're still doing it the beginner (long) way, so while I could use that code I would be just copy pasting it in without actually understanding it which would defeat the purpose. For example were doing ....
For I = 1 To Len(MyStr)
like so
For i = 0 To txtNumber.text.length
Now you can take it to the top floor. Amazing how much difference there is between VB.Net and the rest of all BASIC. Let the boys upstairs translate my code in Post #3 to Vb.Net for you. That's another pure blunder by Microsoft. :sick:
Re: Convert dtring to an Int array?
Quote:
Originally Posted by
Code Doc
That's a shame. It's a piece of cake in VB6. Why not let us solve OP's problem here?:sick:
Short answer. Syntax is not nearly the same. The VB6 solution can lead to more confusion and probably won't work; unless of course, you did post a .Net solution. For example, .Net has many string functions and one probably does what the OP wants.
Re: Convert dtring to an Int array?
Quote:
Originally Posted by
LaVolpe
Short answer. Syntax is not nearly the same. The VB6 solution can lead to more confusion and probably won't work; unless of course, you did post a .Net solution. For example, .Net has many string functions and one probably does what the OP wants.
+1. See my post #9. Hard to believe there is so much difference. :cry:
I suppose it would be interesting to see if .Net can do it in 5 rather simple lines or less. I could reduce it to 3 with colons and get criticized. However, .Net is a far more "robust" language and the language of the future... I yield to the powers in Redmond.
Re: Convert dtring to an Int array?
Without knowing what you've covered so far it's difficult to show a suitable example. I'm no .NET Programmer but here is a solution using the Substring method
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumbers As String = "13579"
Dim myIntegers As New ArrayList
Dim loopControl As Integer
For loopControl = 0 To myNumbers.Length - 1
myIntegers.Add(myNumbers.Substring(loopControl, 1))
Debug.Print(myIntegers(loopControl).ToString)
Next
End Sub
End Class
EDIT: I guess you may not have covered ArrayList so here's a version using a static Integer Array
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumbers As String = "13579"
Dim myIntegers(myNumbers.Length - 1) As Integer
Dim loopControl As Integer
For loopControl = 0 To myNumbers.Length - 1
myIntegers(loopControl) = myNumbers.Substring(loopControl, 1)
Debug.Print(myIntegers(loopControl).ToString)
Next
End Sub
End Class
Re: Convert dtring to an Int array?
Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
Quote:
Originally Posted by
LaVolpe
Notified moderators to move it for you.
Thanks for letting us know :)
Re: Convert dtring to an Int array?
This could be shorter IF it was certain that the string only contained integers
Code:
Dim testString As String = "12345AB67890"
'working with a list in this case
Dim ints As New List(Of Integer)
For Each c As Char In testString 'examine each character
ints.Add(New Integer) 'add a palce holder for TryParse
If Not Integer.TryParse(c, ints(ints.Count - 1)) Then
Debug.WriteLine(c & " is not an Integer")
ints.RemoveAt(ints.Count - 1) 'remove the bad one
End If
Next
Dim intsA() As Integer = ints.ToArray
Re: Convert dtring to an Int array?
Thanks guys! The substring method is what I needed. Data validation to make sure the string contains nothing but ints was done in a secondary function and if not the program will display and error message in a message box to the user to retype some inputs.
Re: [RESOLVED] Convert dtring to an Int array?
Possibly the simplest way, but I doubt you will have covered this in your course:
vb.net Code:
Dim chrArray As Char() = TextBox1.Text.ToCharArray
Dim intArray As Integer() = Array.ConvertAll(chrArray, Function(c As Char) Integer.Parse(c))
Just another suggestion to add to the mix.
Re: [RESOLVED] Convert dtring to an Int array?
Quote:
Originally Posted by
J-Deezy
Possibly the simplest way, but I doubt you will have covered this in your course:
vb.net Code:
Dim chrArray As Char() = TextBox1.Text.ToCharArray
Dim intArray As Integer() = Array.ConvertAll(chrArray, Function(c As Char) Integer.Parse(c))
Just another suggestion to add to the mix.
This is what I was alluding to in my previous post.
In this case, using Substring, is the last approach I would have taken.
But
For Each
his own
Next
:wave:
Re: [RESOLVED] Convert dtring to an Int array?
Quote:
Originally Posted by
dbasnett
But
For Each
his own
Next
:wave:
I see what you did there :cool:
Re: [RESOLVED] Convert dtring to an Int array?
Hmm, I modified it slightly to only accommodate integers in the string so it won't crash if someone puts a stray letter in there.
vb.net Code:
Dim chrArray As Char() = TextBox1.Text.ToCharArray.Where(Function(c As Char) IsNumeric(c)).ToArray
Dim intArray As Integer() = Array.ConvertAll(chrArray, Function(c As Char) Integer.Parse(c))