VCS_String.bas 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Attribute VB_Name = "VCS_String"
  2. Option Compare Database
  3. Option Private Module
  4. Option Explicit
  5. '--------------------
  6. ' String Functions: String Builder,String Padding (right only), Substrings
  7. '--------------------
  8. ' String builder: Init
  9. Public Function Sb_Init() As String()
  10. Dim x(-1 To -1) As String
  11. Sb_Init = x
  12. End Function
  13. ' String builder: Clear
  14. Public Sub Sb_Clear(ByRef sb() As String)
  15. ReDim Sb_Init(-1 To -1)
  16. End Sub
  17. ' String builder: Append
  18. Public Sub Sb_Append(ByRef sb() As String, ByVal Value As String)
  19. If LBound(sb) = -1 Then
  20. ReDim sb(0 To 0)
  21. Else
  22. ReDim Preserve sb(0 To UBound(sb) + 1)
  23. End If
  24. sb(UBound(sb)) = Value
  25. End Sub
  26. ' String builder: Get value
  27. Public Function Sb_Get(ByRef sb() As String) As String
  28. Sb_Get = Join(sb, "")
  29. End Function
  30. ' Pad a string on the right to make it `count` characters long.
  31. Public Function PadRight(ByVal Value As String, ByVal Count As Integer) As String
  32. PadRight = Value
  33. If Len(Value) < Count Then
  34. PadRight = PadRight & Space$(Count - Len(Value))
  35. End If
  36. End Function
  37. ' returns substring between e.g. "(" and ")", internal brackets ar skippped
  38. Public Function SubString(ByVal p As Integer, ByVal s As String, ByVal startsWith As String, _
  39. ByVal endsWith As String) As String
  40. Dim start As Integer
  41. Dim cursor As Integer
  42. Dim p1 As Integer
  43. Dim p2 As Integer
  44. Dim level As Integer
  45. start = InStr(p, s, startsWith)
  46. level = 1
  47. p1 = InStr(start + 1, s, startsWith)
  48. p2 = InStr(start + 1, s, endsWith)
  49. Do While level > 0
  50. If p1 > p2 And p2 > 0 Then
  51. cursor = p2
  52. level = level - 1
  53. ElseIf p2 > p1 And p1 > 0 Then
  54. cursor = p1
  55. level = level + 1
  56. ElseIf p2 > 0 And p1 = 0 Then
  57. cursor = p2
  58. level = level - 1
  59. ElseIf p1 > 0 And p1 = 0 Then
  60. cursor = p1
  61. level = level + 1
  62. ElseIf p1 = 0 And p2 = 0 Then
  63. SubString = vbNullString
  64. Exit Function
  65. End If
  66. p1 = InStr(cursor + 1, s, startsWith)
  67. p2 = InStr(cursor + 1, s, endsWith)
  68. Loop
  69. SubString = Mid$(s, start + 1, cursor - start - 1)
  70. End Function