ClassMicrosoft.VisualBasic.Strings SyntaxDim result = String = Mid(str, start[, length])
DescriptionThe Mid function returns a substring of a specified length from a given string. Usage at a Glance
ExampleThe following example extracts the final directory name from a file path. For instance, in the path c:\folder1\folder2\file.txt, the function returns "folder2." Public Function FinalDirectory(ByVal filePath As String) As String ' ----- Return the last directory name. Dim lastSlash As Integer Dim nextSlash As Integer ' ----- Check for missing data. If (Trim(filePath) = "") Then Return "" ' ----- Don't accept UNC paths. If (VisualBasic.Left(filePath, 1) = "\") Then Return "" ' ----- Find the slashes that surround the directory. lastSlash = InStrRev(filePath, "\") nextSlash = InStrRev(filePath, "\", lastSlash - 1) If (nextSlash = 0) Then Return "" ' ----- Extract and return the name. Return Mid (filePath, nextSlash + 1, lastSlash - nextSlash - 1) End Function See AlsoLeft Function, Mid Function, Right Function |