Share via


The Split Function

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The Split function takes a string and converts it into an array of strings. By default, it divides the string into elements by using the space character as a delimiter, so that if you pass in a sentence, each element of the array contains a word. For example, if you pass this string to the Split function

"This is a test"

you'll get an array that contains the following four elements:

"This"
"is"
"a"
"test"

You can specify that the Split function split the string based on a different delimiter by passing in the delimiter argument.

When you've split a string into an array, it's easy to work with the individual elements. The Split function sizes the array for you, so you don't have to worry about maintaining the array's size.

The following example uses the Split function to count the number of words in a string. The procedure takes a string and returns a long integer indicating the number of words found. Because the string is divided into elements at the space between each word, each element of the resulting array represents a word. To determine the number of words, you simply must determine the number of elements in the array. You can do this by subtracting the lower bound from the upper bound and adding 1.

Function CountWords(strText As String) As Long
   ' This procedure counts the number of words in a string.

   Dim astrWords() As String

   astrWords = Split(strText)
   ' Count number of elements in array -- this will be the
   ' number of words.
   CountWords = UBound(astrWords) - LBound(astrWords) + 1
End Function

See Also

Working with Strings as Arrays | The Join Function | The Filter Function