Discovering Arrays, Page 9
There are two extra Visual Basic functions you might find amazingly useful; Split and Join.
- Split Takes a string, such as 'This is my sentence' and splits it up into individual array elements, depending on a 'delimiter'. In this case, the delimiter could be a space (" "), meaning we get an array of four elements containing This, Is, My, Sentence.
- Join This does the opposite of Split. It takes data from an array and shoves it all together into one long string, with an optional delimiter (which separates the bits of information from each other). So, if we used this on the array created above with a space delimiter (" "), it would take This, Is, My, Sentence and throw them all together, adding spaces in between. In other words, we get back our original sentence 'This is my sentence'.
These concepts always seem exceptionally weird when explained like this - so let's conclude this section with a geek peek at some sample code:
Private Sub Command1_Click()Dim strFirstText As StringDim strSecondText As StringDim StringArray() As StringstrFirstText = "This is my sentence"' First, the splitStringArray = Split(strFirstText, " ")For i = LBound(StringArray) To UBound(StringArray)MsgBox StringArray(i)Next i' And now the joinstrSecondText = Join(StringArray, " ")MsgBox strSecondText End Sub
Page 9 of 11