VCS_String.bas 2.2 KB

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