String into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ldazaa1
    New Member
    • Feb 2008
    • 1

    #1

    String into an array

    Hello all,
    I'm trying to do a small application that has this string as an imput "11010" and my goal is to divide the string and load it into an array in this way:
    [1][11][110][11010]
    The code that I came up with is this:

    [CODE=vbnet]Public Class Form1
    Dim prefixes(10000) As String
    Dim s As String
    Dim count As Integer
    Dim j As Integer
    Dim pre_lenght As Integer
    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    TextBox1.Text = "11010"
    s = TextBox1.Text
    pre_lenght = TextBox1.TextLe ngth
    j = pre_lenght
    While j > 0
    For Me.count = 0 To count = pre_lenght
    prefixes(Me.cou nt) = s.Substring(pre _lenght - j)
    j = j - 1
    Next
    End While
    Label1.Text = j
    Label2.Text = pre_lenght
    Label3.Text = prefixes(0)
    End Sub
    End Class[/CODE]

    For some reason it is just storing information in the position 0 of prefixes() and not in any other position,
    If someone have any idea of how to solve this problem please let me know.
    Thanks.
    Daniel.
    Last edited by Killer42; Feb 8 '08, 01:44 AM. Reason: Added CODE=vbnet tag
  • VBWheaties
    New Member
    • Feb 2008
    • 145

    #2
    Originally posted by ldazaa1
    Hello all,
    I'm trying to do a small application that has this string as an imput "11010" and my goal is to divide the string and load it into an array in this way:
    [1][11][110][11010]
    The code that I came up with is this:

    Public Class Form1
    Dim prefixes(10000) As String
    Dim s As String
    Dim count As Integer
    Dim j As Integer
    Dim pre_lenght As Integer
    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    TextBox1.Text = "11010"
    s = TextBox1.Text
    pre_lenght = TextBox1.TextLe ngth
    j = pre_lenght
    While j > 0
    For Me.count = 0 To count = pre_lenght
    prefixes(Me.cou nt) = s.Substring(pre _lenght - j)
    j = j - 1
    Next
    End While
    Label1.Text = j
    Label2.Text = pre_lenght
    Label3.Text = prefixes(0)
    End Sub
    End Class

    for some reason it is just storing information in the position 0 of prefixes() and not in any other position,
    If someone have any idea of how to solve this problem please let me know.
    Thanks.
    Daniel.
    likely your culprit:

    Code:
    For Me.count = 0 To count = pre_lenght

    Comment

    • WinblowsME
      New Member
      • Jan 2008
      • 58

      #3
      Do you want to store [1101] as well?

      [CODE=vb]
      ' VBA

      Sub Test()
      Dim curr_line As String, i As Integer

      curr_line = "11010"

      For i = 1 To Len(curr_line)
      Debug.Print Mid(curr_line, 1, i)
      Next i
      End Sub[/CODE]

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        wouldnt it be easier with something like:

        [CODE=vb]dim Str1 as string = "11010"
        dim Arr() as string
        dim i as long
        redim arr(1 to len(str1))
        for i = 1 to len(str1)
        arr(i) = left(str1,i)
        next[/CODE]

        HTH

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Sorry kadghar, no Left() function any more, remember? Bummer.

          Idazaa1 would need to use the substring one instead.

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by Killer42
            Sorry kadghar, no Left() function any more, remember? Bummer.

            Idazaa1 would need to use the substring one instead.
            lol

            sorry, instead of left(string,int eger)

            you can always use mid(string, 1, integer)

            happy now?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by kadghar
              you can always use mid(string, 1, integer)

              happy now?
              Not really. That's VB6. Don't think it'll work in the version under discussion. :P

              Comment

              • kadghar
                Recognized Expert Top Contributor
                • Apr 2007
                • 1302

                #8
                Originally posted by Killer42
                Not really. That's VB6. Don't think it'll work in the version under discussion. :P
                that's true, i forgot MID wasnt good anymore. It'll take time till i get used to Substring

                Then instead of Left(string, length), it should be string.substrin g(1,length), right?

                =( it's like learning to walk again.

                Comment

                Working...