string arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vishalgupta
    New Member
    • Jun 2007
    • 47

    string arrays

    i have a string and want to split it at the spaces so i used the following code from the microsoft site:

    Code:
    Dim TestString As String = "apple    pear banana  "
    Dim TestArray() As String = Split(TestString)
    ' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
    Dim LastNonEmpty As Integer = -1
    For i As Integer = 0 To TestArray.Length - 1
        If TestArray(i) <> "" Then
            LastNonEmpty += 1
            TestArray(LastNonEmpty) = TestArray(i)
        End If
    Next
    ReDim Preserve TestArray(LastNonEmpty)
    ' TestArray now holds {"apple", "pear", "banana"}
    i m using visual basic 6.0. the variables cudnt be initialized where they were difined so i manipulated the code as

    Code:
    Dim TestString As String
    TestString = "apple    pear banana  "
    Dim TestArray() As String
    TestArray() = Split(TestString)
    ' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
    Dim LastNonEmpty As Integer
    Dim i As Integer
    LastNonEmpty = -1
    For i = 0 To TestArray.Length - 1               'THE ERROR LINE
        If TestArray(i) <> "" Then
            LastNonEmpty = LastNonEmpty + 1
            TestArray(LastNonEmpty) = TestArray(i)
            MsgBox TestArray(LastNonEmpty)
        End If
    Next
    ReDim Preserve TestArray(LastNonEmpty)
    ' TestArray now holds {"apple", "pear", "banana"}
    y does this happen?? and it still shows invalid wualifier for TestArray.Lengt h..is there any other way to find the length so dat we cud run the loop ftill dat value only???? i did try len(TestArray) and GetLength(TestA rray) but to no avail
  • yzlin04
    New Member
    • Jul 2007
    • 18

    #2
    maybe that code is only for VB.net
    because i try to run the code (from microsoft), with a bit changes by add a listbox (ListBox1) into the form:

    Dim TestString As String = "apple pear banana "
    Dim TestArray() As String = Split(TestStrin g)
    ' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
    Dim LastNonEmpty As Integer = -1
    For i As Integer = 0 To TestArray.Lengt h - 1
    If TestArray(i) <> "" Then
    LastNonEmpty += 1
    TestArray(LastN onEmpty) = TestArray(i)
    ListBox1.items. add(TestArray(i ))
    End If
    Next
    ReDim Preserve TestArray(LastN onEmpty)
    ' TestArray now holds {"apple", "pear", "banana"}

    it works







    Originally posted by vishalgupta
    i have a string and want to split it at the spaces so i used the following code from the microsoft site:

    Code:
    Dim TestString As String = "apple    pear banana  "
    Dim TestArray() As String = Split(TestString)
    ' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
    Dim LastNonEmpty As Integer = -1
    For i As Integer = 0 To TestArray.Length - 1
        If TestArray(i) <> "" Then
            LastNonEmpty += 1
            TestArray(LastNonEmpty) = TestArray(i)
        End If
    Next
    ReDim Preserve TestArray(LastNonEmpty)
    ' TestArray now holds {"apple", "pear", "banana"}
    i m using visual basic 6.0. the variables cudnt be initialized where they were difined so i manipulated the code as

    Code:
    Dim TestString As String
    TestString = "apple    pear banana  "
    Dim TestArray() As String
    TestArray() = Split(TestString)
    ' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
    Dim LastNonEmpty As Integer
    Dim i As Integer
    LastNonEmpty = -1
    For i = 0 To TestArray.Length - 1               'THE ERROR LINE
        If TestArray(i) <> "" Then
            LastNonEmpty = LastNonEmpty + 1
            TestArray(LastNonEmpty) = TestArray(i)
            MsgBox TestArray(LastNonEmpty)
        End If
    Next
    ReDim Preserve TestArray(LastNonEmpty)
    ' TestArray now holds {"apple", "pear", "banana"}
    y does this happen?? and it still shows invalid wualifier for TestArray.Lengt h..is there any other way to find the length so dat we cud run the loop ftill dat value only???? i did try len(TestArray) and GetLength(TestA rray) but to no avail

    Comment

    • vishalgupta
      New Member
      • Jun 2007
      • 47

      #3
      it is not working . i am working in vb 6.0 and i havent done vb.net so i cant comment on dat. the code is having same errors:
      1) it shows TestArray as invalid qualifier
      2)the variables cannot be iinitialized where they r defined

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        In VB6, an array doesn't have attributes like .Length - that's from other languages (like Java, or its close cousin, VB.Net). What you need to use is the Ubound() function.

        Comment

        • vishalgupta
          New Member
          • Jun 2007
          • 47

          #5
          thnks a lot ...can u tell me any place or link where we can search funtions by the job they do and not by their names???lest i wud have found ubound() myself

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by vishalgupta
            thnks a lot ...can u tell me any place or link where we can search funtions by the job they do and not by their names???lest i wud have found ubound() myself
            I think the MSDN website has some such reference, but it might take a while to track down the right place to browse. There's a lot of information there.

            Also, I've just tried a Google search on "vb functions by category" and there were a few hits that looked as though they might be relevant. I didn't take the time to look at more than one or two, though.

            Perhaps we could start such a list here, in our Articles section. (And we could add info on how to achieve similar things in VB and VB.Net)

            Keep in mind, a bit of creative searching will often find what you want. For example, if you were to search TheScripts for something like "VB ARRAY SIZE" you would probably find some relevant entries, but you may need to spend some time reading through the discussions.

            Comment

            • vishalgupta
              New Member
              • Jun 2007
              • 47

              #7
              Originally posted by Killer42
              I think the MSDN website has some such reference, but it might take a while to track down the right place to browse. There's a lot of information there.

              Also, I've just tried a Google search on "vb functions by category" and there were a few hits that looked as though they might be relevant. I didn't take the time to look at more than one or two, though.

              Perhaps we could start such a list here, in our Articles section. (And we could add info on how to achieve similar things in VB and VB.Net)

              Keep in mind, a bit of creative searching will often find what you want. For example, if you were to search TheScripts for something like "VB ARRAY SIZE" you would probably find some relevant entries, but you may need to spend some time reading through the discussions.


              thnks a lot for replying...,

              Comment

              Working...