I'm looking to break up a value of a String

Currymunch

Member
Joined
Jan 21, 2008
Messages
13
Programming Experience
Beginner
Just started to learn how do all this, quite fun but slow going >_<

I was looking for help with the following exercise:

I'm looking to break up a value of a String: for instance 123, so that it is entered into an array that i resized during initialization so that element 0 = 1 element 1 = 2 element 2 = 3. This is so i can perform numeric operations on the array later

'responds to a button click
----------------------------------------

Dim numDigits As Integer
Dim Digits() As Integer
Dim i As Integer


Dim total As Integer
Dim Max As Integer
Dim temp As String

'**********************************************
'*
'*Resizes the array by finding the length of the string
'*Converts to integer and plugs into array
'*
'**********************************************

numDigits = Len(numEnt.Text)
ReDim Digits(numDigits - 1)
temp = Val(numEnt.Text)


---------------------------------
How do i go about breaking the String numEnt.Text (the textbox on the form) up and entering into the Digits array
 
here's how to do it the way you want:

Sub CharArrayTest()

Dim s() As String = GetCharArray("12345")

For i As Integer = 0 To UBound(s)
MsgBox(s(i))
Next

End Sub
Function GetCharArray(ByVal s As String) As String()

Dim CharArray(s.Length - 1) As String

For i As Integer = 1 To s.Length
CharArray(i) = Mid(s, i, 1)
Next

End Function

however, if you can delimit your string (using commas, for example) then you can split your string into an array with the split function:

Sub SplitTest()

Dim s() As String = Split("1,2,3,4,5", ",")

End Sub

good luck
 
Another approach to the problem

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mynum As String = "615612529174564"
Dim L As Integer = mynum.Length
Dim showme As String = ""
Dim numarray(L - 1) As Integer
For x As Integer = 0 To L - 1
numarray(x) = Convert.ToInt32(mynum.Substring(x, 1))
showme &= numarray(x).ToString & vbCrLf
Next
MessageBox.Show(showme)
End Sub
 
Thanks for the pointers, this is loads of fun learning this stuff. Just enrolled in a new degree after finishing engineering and hating it - had more fun with this stuff than in 4 years of eng :( . I ended up using the mod function so that it returns a value when / 10:


i = 0
Do While temp > 0
remDiv = temp Mod 10
Digits(i) = remDiv
temp = (temp - remDiv) / 10
i = i + 1
Loop

thus 123 becomes 321 in the array, bit sketchy but that how it was given on the slides thus far in lectures.

Have to return the value of the index of the largest element in the array next -- head scratching time :p

Max = Digits(0)
For i = 1 To UBound(Digits)
If Digits(i) > Max Then
Max = Digits(i)
End If
Next

f3.lblMaxIndex.Text = "The Index of the largest value is " + Str(Max)


Thats what ive got thus far, it returns the max value -- any pointers on how you would find out the elements of the results that are found above ^^?
 
Just note that Mod is an operator, NOT a function.

In your code above you've got a variable declared to store the max value but you have no variable for the index of the max value, which is the value you actually need. You start using an index of zero to get the first element, so obviously you start with an index of zero. Each iteration of the loop you test the element at index i against your max value and save that element if it's greater. Where do you save the index of that element, given that that is the value you're ultimately interested in?
 
I swear forums > lectures with regards to clarity. Sorry for the mod operator naming - new at all this.

I tried what you said and you were spot on:

Dim index As Integer
Dim maxIndex
maxIndex = 0
Max = Digits(0)
For index = 1 To UBound(Digits)
If Digits(index) > Max Then
Max = Digits(index)
maxindex = index
End If
Next

f3.lblMaxIndex.Text = "The largest value is " + Str(Max)
f3.lblIndex.Text = "The index is " + Str(maxIndex)


---------------------------------------

Thanks so much for the help, expect many more silly questions and mistakes in the future.
 
Back
Top