Browse DevX
Sign up for e-mail newsletters from DevX


The Many Uses of the Split Function  : Page 2

The Split function can provide unexpected returns if used properly.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

So, What's the Big Deal with the Split Function?
You can use the Split function to quickly parse a text string and break it into an array. Working with an array is easier if you need to access a particular value quickly or iterate through all values. You can therefore replace all the following code, with the one line Split function we used above.

Dim strValue
Dim strAryWords()
Dim i, iCount
strValue = "This is the tactical ASP column by Rama Ramachandran"

i = Instr(strValue, " ")
iCount = 0
Do While i > 0
	Redim Preserve strAryWords(iCount)
	strAryWords(iCount) = Left(strValue, i-1)
	iCount = iCount + 1
	strValue = Mid(strValue, i+1)

	i = Instr(strValue, " ")
If strValue <> "" Then
	Redim Preserve strAryWords(iCount)
	strAryWords(iCount) = strValue
End if
' - NOW, strAryWords is an array of all the words in the sentence.
' - All the above code can be replaced by one function call
strAryWords = Split(strValue, " ")

You can see how the built-in function can be faster and more efficient than a lot of interpreted code, especially in an ASP application.

What the documentation does not indicate is that the Split function does not just use a single character as a delimiter—this is where its myriad uses pop up. You can use any text string as a delimiter with the Split function. Let us see how we can use this.

Suck in an Entire File from Disk into a Memory Array
There is one use of the Split function that I bet most of you did not think of or know about. The Split function can be used to suck in an entire file from disk into a neat memory array in one fell swoop. Let's see how we used to do it and see how we can do it now.

This is the old way (pseudocode):
Open a File
Read one Line
Place the line into the memory array as an item
Read the next line
Till we are at end of the file
Close the file

This is the new way (pseudocode):
Open the file
Read entire file into a text string in memory
Close the file
Split entire file into its separate lines, placing each line into memory array as an item—using the Split function

And here's the code for a Visual Basic application:

Dim iFile as integer
Dim strFileBuff as string
Dim strAryLines() as string

' - Get a handle for the file
iFile = FreeFile
' - Open the file
Open "C:\FileName.txt" for input as #iFile
' - Read entire file into a text string in memory
strFileBuff = Input(LOF(iFile), #iFile)
' - close the file
Close #iFile
' - split entire file, placing each line into array as an item
strAryLines = Split(strFileBuff, vbCrLF)

The magic happens at the very last line. Before that, we have sucked an entire file from disk into a memory variable using the single line of code:

strFileBuff = Input(LOF(iFile), #iFile)

This code reads an entire line into a memory variable (I tested this with a 25MB text file containing 25 million characters separated into 118,000 lines on a machine with 128MB RAM, running Win2000 Professional with no problems—it read the file in under two seconds). After closing the file, the Split function uses the end-of-line characters (Carriage return and Line Feed, or vbCrLF) to split the memory variable into an array of lines from the file in memory—again, in under two seconds. At the end of the above code, you have every line from the file in your memory array that you can then manipulate. You can find the total number of lines in the file by using the UBound function on the Array variable, and you can access any line by using the Array Name (item number) notation.

To use the code in an ASP page, you use the FileSystemObject to read a text file and use the ReadAll method to read the entire contents of the file into a variable. Then use the function to split it into an array.

Count the Number of Times a Word Occurs
You can use the same split function to also count the number of times a word occurs in a file. Using the same code as above, we can read the file into a memory text variable. Instead of using the vbCrLF characters to split the variable into an array, use the word you are counting for. For example, to find out the number of times the word "cool" appears in a text file, use this code:

' - split entire file, placing each line into array as an item
strAryLines = Split(strFileBuff, "cool")

Then you can find out the number by using this line of code:

intCount = Ubound(strAryLines)

You can also use the same technique to count the number of times a word occurs within the content of a record. Just substitute the file contents with your record contents. Assuming you had opened a recordset called objRS, you could use the following code:

' - split entire file, placing each line into array as an item
strAryLines = Split(objRS("FieldName"), "cool")
intCount = Ubound(strAryLines)

Rank Your Records
If you are using Access as your back-end database, you can use the Split function within an Access query to return records that contain the words "cool" ranked in order based on the number of times the word "cool" occurs within the text. To do so, use the following code to create a generic function that returns a count of the number of times a word occurs within another text string (your field). As you can see, this function uses the split function to break the text apart at the word and returns the count:

Function GetCount(ByVal strText, ByVal strWord)
    GetCount = UBound(Split(strText, strWord))
End Function

Then use the GetCount function within your query to bring back ranked records. Use the following SQL as your query. The reason why it will work in Access is that Access supports the UBound and the Split function. If you try this query within any other database, such as SQL Server, you will not be able to use the Split and UBound functions within the query itself.

SELECT Table1.Field1, Table1.Field2...
FROM Table1
WHERE Table1.Field1 LIKE "*cool*"
ORDER BY GetCount(Table1.Field1, "cool") DESC;

Rama Ramachandran is the Director of Technology with Imperium Solutions and is a Microsoft Certified Solution Developer and Site Builder. He has extensive experience with building database systems and has co-authored several books including Professional Visual InterDev 6 Programming and Professional Data Access (Wrox). Rama also teaches Visual Basic and Web Development at Fairfield University and University of Connecticut.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date