-
Feb 5th, 2011, 03:01 PM
#1
Thread Starter
New Member
[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.
-
Feb 5th, 2011, 03:23 PM
#2
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.
-
Feb 5th, 2011, 08:35 PM
#3
Re: Convert dtring to an Int array?
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?
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
Last edited by Code Doc; Feb 5th, 2011 at 08:42 PM.
Doctor Ed
-
Feb 5th, 2011, 08:41 PM
#4
Thread Starter
New Member
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
-
Feb 5th, 2011, 08:43 PM
#5
Re: Convert dtring to an Int array?
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.
-
Feb 5th, 2011, 08:44 PM
#6
Thread Starter
New Member
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...
-
Feb 5th, 2011, 08:54 PM
#7
Thread Starter
New Member
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
-
Feb 5th, 2011, 08:55 PM
#8
Re: Convert dtring to an Int array?
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).
-
Feb 5th, 2011, 09:01 PM
#9
Re: Convert dtring to an Int array?
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.
-
Feb 5th, 2011, 09:02 PM
#10
Re: Convert dtring to an Int array?
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?
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.
-
Feb 5th, 2011, 09:51 PM
#11
Re: Convert dtring to an Int array?
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.
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.
Last edited by Code Doc; Feb 5th, 2011 at 10:05 PM.
Doctor Ed
-
Feb 6th, 2011, 02:18 AM
#12
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
Last edited by Doogle; Feb 6th, 2011 at 04:12 AM.
-
Feb 6th, 2011, 05:20 AM
#13
Re: Convert dtring to an Int array?
Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum
Originally Posted by LaVolpe
Notified moderators to move it for you.
Thanks for letting us know
-
Feb 6th, 2011, 10:26 AM
#14
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
-
Feb 6th, 2011, 10:32 AM
#15
Thread Starter
New Member
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.
-
Feb 6th, 2011, 11:39 AM
#16
Fanatic Member
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.
If I helped you out, please take the time to rate me
-
Feb 6th, 2011, 11:55 AM
#17
Re: [RESOLVED] Convert dtring to an Int array?
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
-
Feb 6th, 2011, 11:58 AM
#18
Fanatic Member
Re: [RESOLVED] Convert dtring to an Int array?
Originally Posted by dbasnett
But
For Each
his own
Next
I see what you did there
If I helped you out, please take the time to rate me
-
Feb 7th, 2011, 04:03 AM
#19
Fanatic Member
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))
If I helped you out, please take the time to rate me
Tags for this Thread
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
|