home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD41343222000.psc / ntImgToIcon / modStringOps.bas < prev    next >
Encoding:
BASIC Source File  |  2000-03-16  |  13.5 KB  |  335 lines

  1. Attribute VB_Name = "modStringOps"
  2. Option Explicit
  3.  
  4. 'This file was created 2/13/00
  5. 'by Shannon Little
  6. 'http://go.to/neotrix
  7. 'This contains all of my functions to format and do operations on strings
  8. 'and numbers
  9.  
  10. 'Private Declare Function StrFormatByteSize Lib "shlwapi" Alias "StrFormatByteSizeA" ( _
  11.             'ByVal dw As Long, _
  12.             'ByVal pszBuf As String, _
  13.             'ByRef cchBuf As Long) As String
  14.  
  15.  
  16. Enum ByteScale
  17.     bsKb = 0
  18.     bsMb = 1
  19.     bsGb = 2
  20.     bsTb = 3
  21. End Enum
  22.  
  23. Enum ConvertScale
  24.     csByte = 4
  25.     csKb = 3
  26.     csMb = 2
  27.     csGb = 1
  28.     csTb = 0
  29. End Enum
  30.  
  31.  
  32. 'Created 2/13/00
  33. Public Function CapFirstLetter(ByVal strString As String) As String
  34.     'Returns a string with just the first letter capitalized
  35.     CapFirstLetter = UCase(Left(strString, 1)) & Right(strString, Len(strString) - 1)
  36. End Function
  37.  
  38. 'Created 2/13/00
  39. Public Function StrReturnRight(ByVal strString As String, ByVal strKeyValue As String) As String
  40.     'Check for a key value, such as "." and returns everything to the
  41.     'right of it, starts at the first occurence
  42.     Dim N As Integer
  43.     N = InStr(1, strString, strKeyValue, vbBinaryCompare)    'Returns the position of first occurence
  44.     
  45.     If N = 0 Then  '0 means is isn't in there, return original string
  46.         StrReturnRight = strString
  47.         Exit Function
  48.     Else
  49.         StrReturnRight = Right(strString, Len(strString) - N)
  50.     End If
  51.     
  52.     'sdfs.ext
  53.     '12345678
  54.     'N=5, return 6 to 8
  55. End Function
  56.  
  57. 'Created 3/10/00
  58. 'Check for a key value, such as "." and returns everything to the
  59. 'right of it. It starts searching from the end of the string
  60. Public Function StrReturnRightFromEnd(ByVal strString As String, ByVal strKeyValue As String) As String
  61.     Dim N As Integer
  62.       
  63.     N = InStrRev(strString, strKeyValue, , vbBinaryCompare)   'Returns the position of first occurence from the end
  64.     
  65.     If N = 0 Then  '0 means is isn't in there, return original string
  66.         StrReturnRightFromEnd = strString
  67.         Exit Function
  68.     Else
  69.         StrReturnRightFromEnd = Right(strString, Len(strString) - N)
  70.     End If
  71. End Function
  72.  
  73. 'Created 2/13/00
  74. Public Function StrReturnLeft(ByVal strString As String, ByVal strKeyValue As String) As String
  75.     'Check for a key value, such as "." and returns everything to the
  76.     'left of it, starts at the first occurence
  77.     Dim N As Integer
  78.     N = InStr(1, strString, strKeyValue, vbBinaryCompare)
  79.     
  80.     If N = 0 Then  '0 means is isn't in there, return original string
  81.         StrReturnLeft = strString
  82.         Exit Function
  83.     Else
  84.         StrReturnLeft = Left(strString, N - 1)
  85.     End If
  86. End Function
  87.  
  88. 'Created 3/10/00
  89. 'Check for a key value, such as "." and returns everything to the
  90. 'right of it. It starts searching from the end of the string
  91. Public Function StrReturnLeftFromEnd(ByVal strString As String, ByVal strKeyValue As String) As String
  92.     'Check for a key value, such as "." and returns everything to the
  93.     'left of it, starts at the first occurence
  94.     Dim N As Integer
  95.     N = InStrRev(strString, strKeyValue, , vbBinaryCompare)   'Returns the position of first occurence from the end
  96.     
  97.     If N = 0 Then  '0 means is isn't in there, return original string
  98.         StrReturnLeftFromEnd = strString
  99.         Exit Function
  100.     Else
  101.         StrReturnLeftFromEnd = Left(strString, N - 1)
  102.     End If
  103. End Function
  104.  
  105. 'Created 2/13/00
  106. Public Function DoesCharExist(ByVal strStringToSearch As String, ByVal strKey As String) As Boolean
  107.     Dim Result
  108.     'Its defualt search mode is binary, so case does matter
  109.     'Returns TRUE if it exists
  110.     Result = InStr(1, strStringToSearch, strKey)
  111.     DoesCharExist = IIf(Result > 0, True, False)
  112. End Function
  113.  
  114. 'Created 2/23/00
  115. 'Capitializes the first letter in every word
  116. Public Function FormatProperCase(ByVal strString As String) As String
  117.     FormatProperCase = StrConv(strString, vbProperCase)
  118. End Function
  119.  
  120. 'Created 2/26/00
  121. 'Formats the string to a good looking file/folder name
  122. Public Function FormatProperName(ByVal strString As String) As String
  123.     FormatProperName = FormatProperCase(LCase(strString))
  124. End Function
  125.  
  126.  
  127. 'Created 2/13/00
  128. 'I just found out there was already a VB function for this, well this really sucks
  129. 'Public Function CapAfterEverySpace(ByVal strString As String)
  130.     'Returns a string with every letter after a space captalized
  131.     'Dim strFront As String, strMid As String, strEnd As String
  132.     'Dim N As Integer
  133.     
  134.     'For N = 1 To Len(strString)   'Go through each letter
  135.         'If Mid(strString, N, 1) = " " Then    'If we find a space
  136.             'Caps the letter after the space
  137.             'Example (for me)
  138.             'new folder
  139.             '12345678910
  140.             'N=4, So 1 to 4 Add to Ucase(5) Add to 5 to 10
  141.             'strFront = Left(strString, N)
  142.             'strMid = UCase(Mid(strString, N + 1, 1))
  143.             'strEnd = Right(strString, Len(strString) - N - 1)
  144.             'This was my all in 1 line version, but it was too hard to read at a glance
  145.             'strTemp = Left(strTemp, N) & UCase(Mid(strTemp, N + 1, 1)) & Right(strTemp, Len(strTemp) - N - 2)
  146.             'Combine new name and keep looping through checking for more spaces
  147.             'strString = strFront & strMid & strEnd
  148.             'If you uncomment this, you will see exactly how, on each loop, the program does the caps after each space
  149.             'Debug.Print "strTemp: " & strTemp, "Front: " & strFront, "Mid: " & strMid, "End: " & strEnd, "Comp: " & strFront & strMid & strEnd
  150.         'End If
  151.     'Next
  152.     'CapAfterEverySpace = strString
  153. 'End Function
  154.  
  155. 'DO NOT USE, does not handle large file sizes over 1.5 Gb
  156. 'Formats a bytes number to Kb or Mb
  157. 'Public Function FormatSize(ByVal lngAmount As Long) As String
  158.     'Dim strBuffer As String
  159.     'Dim strReturn As String
  160. '
  161.     'strBuffer = Space$(255)
  162.     'strReturn = StrFormatByteSize(lngAmount, strBuffer, Len(strBuffer))
  163. '
  164.     'If InStr(strReturn, vbNullChar) <> 0 Then
  165.         'FormatSize = Left$(strReturn, InStr(strReturn, vbNullChar) - 1)
  166.     'End If
  167. 'End Function
  168.  
  169.  
  170.  
  171. 'Created 2/23/00
  172. 'I made this because the built in Window version did not handle large sizes
  173. 'You must pass a BYTE size only
  174. 'It will then convert it to the best size to display it as, ie MB,GB,TB!
  175. Public Function FormatBytesToBestSize(ByVal Amount, Optional bUseCommas As Boolean, Optional intRoundToPlaces As Integer) As String
  176.     'All variables that handle the numbers are left as variant because
  177.     'The file size can get Very large when expressed as bytes
  178.     
  179.     'If no value is specified, the default will be 2 places
  180.     If IsMissing(intRoundToPlaces) Then intRoundToPlaces = 2
  181.     'If no value is specified, the default will be false
  182.     If IsMissing(bUseCommas) Then bUseCommas = False
  183.     
  184.     If Amount < 1024 Then
  185.         Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  186.         FormatBytesToBestSize = Amount & " Bytes"
  187.     Else
  188.         Amount = Amount / 1024
  189.         If Amount < 1024 Then
  190.             Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  191.             FormatBytesToBestSize = Amount & " Kb"
  192.         Else
  193.             Amount = Amount / 1024
  194.             If Amount < 1024 Then
  195.                 Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  196.                 FormatBytesToBestSize = Amount & " Mb"
  197.             Else
  198.                 Amount = Amount / 1024
  199.                 If Amount < 1024 Then
  200.                     Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  201.                     FormatBytesToBestSize = Amount & " Gb"
  202.                 Else
  203.                     Amount = Amount / 1024
  204.                     If Amount < 1024 Then
  205.                         Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  206.                         FormatBytesToBestSize = Amount & " Tb"
  207.                     End If
  208.                 End If
  209.             End If
  210.         End If
  211.     End If
  212. End Function
  213.  
  214. 'Created 2/24/00
  215. 'This will format a bytes number to the specified type, MB, GB TB!
  216. 'Without adding on what type it is, ie Adding MB to 96 MB
  217. 'It will just return 96
  218. 'I decides to make a function just for Bytes because it is the most (only?)
  219. 'Commonly returned size from window API functions
  220. Public Function FormatByteSize(ByVal Amount, bsScale As ByteScale, Optional bFormatToString As Boolean, Optional bUseCommas As Boolean, Optional intRoundToPlaces As Integer) As String
  221.     'All variables that handle the numbers are left as variant because
  222.     'The file size can get Very large when expressed as bytes
  223.     
  224.     'If no value is specified, the default will be 2 places
  225.     If IsMissing(intRoundToPlaces) Then intRoundToPlaces = 2
  226.     'If no value is specified, the deafult will be false
  227.     If IsMissing(bFormatToString) Then bFormatToString = False
  228.     'If no value is specified, the defaul will be false
  229.     If IsMissing(bUseCommas) Then bUseCommas = False
  230.     
  231.     Select Case bsScale
  232.         Case bsKb:
  233.             Amount = Amount / 1024
  234.             Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  235.             FormatByteSize = IIf(bFormatToString, Amount & " Kb", Amount)
  236.         Case bsMb:
  237.             Amount = Amount / 1024 / 1024
  238.             Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  239.             FormatByteSize = IIf(bFormatToString, Amount & " Mb", Amount)
  240.         Case bsGb:
  241.             Amount = Amount / 1024 / 1024 / 1024
  242.             Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  243.             FormatByteSize = IIf(bFormatToString, Amount & " Gb", Amount)
  244.         Case bsTb:
  245.             Amount = Amount / 1024 / 1024 / 1024 / 1024
  246.             Amount = IIf(bUseCommas, Format(Round(Amount, intRoundToPlaces), "###,###,###.##########"), Round(Amount, intRoundToPlaces))
  247.             FormatByteSize = IIf(bFormatToString, Amount & " Tb", Amount)
  248.     End Select
  249.     
  250. End Function
  251.  
  252. 'Created 2/24/00
  253. 'This function will convert any size (Byte,Kb,Mb,Gb,Tb) to any size (Byte,Kb,Mb,Gb,Tb)
  254. 'Of course some resolution is lost when going from Mb to Byte unless you are using a single number
  255. Public Function ConvertSizeTo(ByVal Amount, bsFromScale As ConvertScale, bsToScale As ConvertScale, Optional intRoundToPlaces As Integer)
  256.     Dim intLevels As Integer
  257.     
  258.     If IsMissing(intRoundToPlaces) Then intRoundToPlaces = 2
  259.     
  260.     'Shows how many levels to go up or down
  261.     'Byte = 4
  262.     'Mb = 2
  263.     '4 - 2 = 2 levels up to go
  264.     'So Amount / 1024 / 1024 = Mb from Bytes
  265.     intLevels = bsFromScale - bsToScale
  266.     
  267.     
  268.     Select Case intLevels
  269.         Case -4: Amount = Amount * 1024 * 1024 * 1024 * 1024
  270.         Case -3: Amount = Amount * 1024 * 1024 * 1024
  271.         Case -2: Amount = Amount * 1024 * 1024
  272.         Case -1: Amount = Amount * 1024
  273.         Case 0: 'Same size in both From and To
  274.         Case 1: Amount = Amount / 1024
  275.         Case 2: Amount = Amount / 1024 / 1024
  276.         Case 3: Amount = Amount / 1024 / 1024 / 1024
  277.         Case 4: Amount = Amount / 1024 / 1024 / 1024 / 1024
  278.     End Select
  279.     ConvertSizeTo = Round(Amount, intRoundToPlaces)
  280. End Function
  281.  
  282. 'Created 2/25/00
  283. 'Returns TRUE if the strSearchForString string in somewhere in the strSourceString string
  284. Public Function IsStringContainedIn(ByVal strSourceString As String, ByVal strSearchForString As String, Optional bCaseSensitive As Boolean) As Boolean
  285.     Dim intSearchType As Integer
  286.     
  287.     'By default case does not matter when searching
  288.     If IsMissing(bCaseSensitive) Then bCaseSensitive = False
  289.     
  290.     'vbBinaryCompare 0 Performs a binary comparison.    'Case sensitive
  291.     'vbTextCompare 1 Performs a textual comparison.     'Not case sensitive
  292.     
  293.     If bCaseSensitive Then
  294.         intSearchType = 0
  295.     Else
  296.         intSearchType = 1
  297.     End If
  298.     
  299.     If InStr(1, strSourceString, strSearchForString, intSearchType) > 0 Then
  300.         IsStringContainedIn = True
  301.     Else
  302.         IsStringContainedIn = False
  303.     End If
  304. End Function
  305.  
  306. 'Created 3/16/00
  307. 'Returns TRUE if the source string contains any chars that are illegal filename chars
  308. 'Check the ASCII number of every letter in the string and makes sure it is
  309. 'within a certain range, if its not the it immediately quits and return TRUE
  310. Public Function DoesContainIllegalChars(ByVal strSource As String) As Boolean
  311.     
  312.  
  313.     DoesContainIllegalChars = False
  314. End Function
  315.  
  316. 'Created 3/14/00
  317. 'Removes the specified string
  318. Public Function RemoveStringFromString(ByVal strSourceString As String, ByVal strSearchString As String, Optional intNumberOfReplacements As Integer, Optional bCaseSensitive As Boolean) As String
  319.     Dim searchCase As Integer
  320.     
  321.     If IsMissing(bCaseSensitive) Then bCaseSensitive = False
  322.     
  323.     If bCaseSensitive Then
  324.         searchCase = 1
  325.     Else
  326.         searchCase = 0
  327.     End If
  328.     
  329.     '-1 is delete every instance
  330.     If IsMissing(intNumberOfReplacements) Then intNumberOfReplacements = -1
  331.     
  332.     RemoveStringFromString = Replace(strSourceString, strSearchString, "", , intNumberOfReplacements, searchCase)
  333. End Function
  334.  
  335.