Attribute VB_Name = "modStrings" Function ValidPhone(strPhone As String) As Boolean ' This procedure checks that the passed-in value is ' a valid, properly formatted telephone number. ValidPhone = strPhone Like "###-###-####" End Function Function ParsePath(strPath As String, _ lngPart As opgParsePath) As String ' This procedure takes a file path and returns ' either the path portion or the file portion, ' depending on which constant was passed in. Dim lngPos As Long Dim strPart As String Dim blnIncludesFile As Boolean ' Check that this is a file path. ' Find the last path separator. lngPos = InStrRev(strPath, "\") ' Determine whether portion of string after last backslash ' contains a period. blnIncludesFile = InStrRev(strPath, ".") > lngPos If lngPos > 0 Then Select Case lngPart ' Return file name. Case opgParsePath.FILE_ONLY If blnIncludesFile Then strPart = Right$(strPath, Len(strPath) - lngPos) Else strPart = "" End If ' Return path. Case opgParsePath.PATH_ONLY If blnIncludesFile Then strPart = Left$(strPath, lngPos) Else strPart = strPath End If ' Return drive. Case opgParsePath.DRIVE_ONLY strPart = Left$(strPath, 3) ' Return file extension. Case opgParsePath.FILEEXT_ONLY If blnIncludesFile Then ' Take three characters after period. strPart = Mid(strPath, InStrRev(strPath, ".") + 1, 3) Else strPart = "" End If Case opgParsePath.FILE_ONLY_NO_EXT If blnIncludesFile Then strPart = Right$(strPath, Len(strPath) - lngPos) strPart = Left$(strPart, Len(strPart) - 4) Else strPart = "" End If Case Else strPart = "" End Select End If ParsePath = strPart ParsePath_End: Exit Function End Function 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 Function CountOccurrences(strText As String, _ strFind As String, _ Optional lngCompare As VbCompareMethod) As Long ' Count occurrences of a particular character or characters. ' If lngCompare argument is omitted, procedure performs binary comparison. Dim lngPos As Long Dim lngTemp As Long Dim lngCount As Long ' Specify a starting position. We don't need it the first ' time through the loop, but we'll need it on subsequent passes. lngPos = 1 ' Execute the loop at least once. Do ' Store position at which strFind first occurs. lngPos = InStr(lngPos, strText, strFind, lngCompare) ' Store position in a temporary variable. lngTemp = lngPos ' Check that strFind has been found. If lngPos > 0 Then ' Increment counter variable. lngCount = lngCount + 1 ' Define a new starting position. lngPos = lngPos + Len(strFind) End If ' Loop until last occurrence has been found. Loop Until lngPos = 0 ' Return the number of occurrences found. CountOccurrences = lngCount End Function Function ReplaceSubstring(strText As String, _ strFind As String, _ strReplace As String) As String ' This function searches a string for a substring and replaces it. ' You can use a wildcard mask to specify the search string. Dim astrText() As String Dim lngCount As Long ' Split the string at specified delimiter. astrText = Split(strText) ' Loop through array, performing comparison ' against wildcard mask. For lngCount = LBound(astrText) To UBound(astrText) If astrText(lngCount) Like strFind Then ' If array element satisfies wildcard search, ' replace it. astrText(lngCount) = strReplace End If Next ' Join string, using same delimiter. ReplaceSubstring = Join(astrText) End Function Function TrimSpace(strInput As String) As String ' This procedure trims extra space from any part of ' a string. Dim astrInput() As String Dim astrText() As String Dim strElement As String Dim lngCount As Long Dim lngIncr As Long ' Split passed-in string. astrInput = Split(strInput) ' Resize second array to be same size. ReDim astrText(UBound(astrInput)) ' Initialize counter variable for second array. lngIncr = LBound(astrInput) ' Loop through split array, looking for ' non-zero-length strings. For lngCount = LBound(astrInput) To UBound(astrInput) strElement = astrInput(lngCount) If Len(strElement) > 0 Then ' Store in second array. astrText(lngIncr) = strElement lngIncr = lngIncr + 1 End If Next ' Resize new array. ReDim Preserve astrText(LBound(astrText) To lngIncr - 1) ' Join new array to return string. TrimSpace = Join(astrText) End Function Function ConvertToProperCase(strText As String) As String ' This function takes a string and converts it to proper ' case, except for any minor words. Dim astrText() As String Dim astrWords() As String Dim astrMatches() As String Dim lngCount As Long ' Return array containing minor words. astrWords = GetMinorWords ' Split string into array. astrText = Split(strText) ' Check each word in passed-in string against array ' of minor words. For lngCount = LBound(astrText) To UBound(astrText) ' Filter function returns array containing matches found. ' If no matches are found, upper bound of array is -1. ' Store result returned by Filter function in a String array, ' then check upper bound. astrMatches = Filter(astrWords, astrText(lngCount)) If UBound(astrMatches) = -1 Then ' If word in string does not match any word in array ' of minor words, convert word to proper case. astrText(lngCount) = StrConv(astrText(lngCount), vbProperCase) Else ' If it does match, convert it to lowercase. astrText(lngCount) = StrConv(astrText(lngCount), vbLowerCase) End If Next ' Join the string. ConvertToProperCase = Join(astrText) End Function Function GetMinorWords() As String() ' Create array containing minor words ' from delimited text file. Dim fsoSysObj As FileSystemObject Dim filFile As File Dim txsStream As TextStream Dim strWords As String Dim strDelim As String Dim astrWords() As String Dim strMsg As String ' Create new FileSystemObject. Set fsoSysObj = New FileSystemObject ' Get file. Set filFile = fsoSysObj.GetFile(CodeProject.Path & _ "\CH07_01.txt") ' Open file for reading. Set txsStream = filFile.OpenAsTextStream(IOMode:=ForReading) ' Read file into string. strWords = txsStream.ReadAll ' Find first possible delimiter. strDelim = GetLikelyDelimiter(strWords) ' Split string into array. astrWords = Split(strWords, strDelim) ' Return array. GetMinorWords = astrWords End Function Function GetLikelyDelimiter(strText As String) As String ' This procedure takes a string and looks for the first ' punctuation character that could be used as a delimiter. Dim abytChars() As Byte Dim lngCount As Long If Len(strText) > 0 Then ' Assign string to byte array. abytChars = strText For lngCount = LBound(abytChars) To UBound(abytChars) If abytChars(lngCount) <> 0 Then Select Case abytChars(lngCount) Case opgDelimiters.COLON, opgDelimiters.COMMA, _ opgDelimiters.CR, opgDelimiters.LF, _ opgDelimiters.PERIOD, opgDelimiters.POUND, _ opgDelimiters.SEMICOLON, opgDelimiters.TABCHAR GetLikelyDelimiter = Chr$(abytChars(lngCount)) Exit For End Select End If Next Else GetLikelyDelimiter = "" End If End Function