Link to home
Start Free TrialLog in
Avatar of ouch_mybrain_

asked on putting string's substring values into an array

Hello Experts,

First of all I am a beginner, I have been actively learning to code for the second day and I like to think im picking it up quickly, if not for the odd stumbling block.

Can someone please help me complete the following code. I am trying to create a small program that accepts a string input and then puts the characters in that string into an array. The code is triggered by the button "Button1" on the form, there is also a text box that allows you to enter the string in the first place.

I know the logic as to how I want it to work (see below), but i'm having trouble putting it into code. Can anyone help or provide advice.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click

        Dim array As New ArrayList

        Dim inputword As String
        inputword = txtBoxGuess.Text

        Dim inputwordlength As Integer
        inputwordlength = inputword.Length

        Dim loopcount As Integer

        '[start loop]
        'add next unprocessed character of string "inputword" to first space allocation in array ("inputword.substring(????)")
        'increment integer "loopcount" by 1
        'stop looping when integer "inputword.length" is the same value as integer "loopcount"

    End Sub
End Class

Open in new window

Many Thanks.
Avatar of mwheeler1982
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Certainly mwheeler1982's option above is the most efficient.
Avatar of ouch_mybrain_


Thank you both, for giving me some good info and lots to think about .... very interested about these "elements", puts the confusion I had about loops to rest.

Thanks again
    Sub Main()

        Dim myString As String = "This is my string!"
        Dim i As Integer = 0

        'Access by index
        'Note that a 5 character string runs from index of 0 - 4, indexes generally start at 0 for first item in an array
        'so we subtract 1 from length for upper bound
        While (i <= (myString.Length - 1))

            Console.WriteLine("myString({0}) = {1}", i, myString(i))
            i = i + 1
        End While

        'Now lets do it the easy way
        'A string is an array of characters
        For Each c As Char In myString

    End Sub

Open in new window

Both of these examples work based off the fact that a string IS an array of characters.  That being said, when at all possible use the provided string functions and I would say .NET provided functions in general when dealing with arrays.  They will help protect you from run-time errors resulting from accessing out of the bounds of the array.  I remember when I was first learning C++ in school that strings could be a pain to deal with thanks to out of bounds access due to a slip in hand coded iteration.

The first example of doing it is based off directly looking up the character based off the index.  This appears to be where you were headed in your code above.

The second example is using the 'For Each' statement.  I would use this over hand coding a counter and iterating through by hand in most all cases.

It allows you to declare a temporary variable, in this case 'c', and repeats the behavior for each instance of a character in the collection of characters.

Also, if you do need to do some array manipulation with strongly typed objects, I'd suggest looking into 'List(of T)' as soon as possible.  That thing is glorious for simplifying lists of just about anything.
Basically mirror posted what Jarnesrh said, that's what happens when I do 6 things at once while typing!
Thanks Memccullock - very helpful comments. I will know going forward about strings pretty much being arrays in themselves.

I'm actually trying to build my own "hang man" game and I needed a way to interrogate characters in a word so I can then put logic in to say whether the player loses a life or not. Not really that interesting but thought it would be a good way to develop my vb skills - and it's doing the trick so far :)

Thanks again everyone