vbAccelerator - Contents of code file: cSplitString.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cSplitString"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit


' =====================================================================
' cSplitString
' SP McMahon (steve@vbaccelerator.com)
' 29 December 1999
' ---------------------------------------------------------------------
'
' A simple class for splitting a string by one or more delimiters,
' complete with native For...Each enumeration support.
'
' Thanks to Bruce McKinney and Paul Wilde for demonstrating the
' code required for For...Each support
'
' ---------------------------------------------------------------------
' http://vbAccelerator.com
' Free, Advanced Source Code for Visual Basic
' =====================================================================
Private m_sString As String
Private m_lStrLen As Long
Private m_sSplitter() As String
Private m_lSplitterLen() As Long
Private m_lSplitterCount As Long
Private m_iThisSplitter As Long
Private m_lLastPos As Long
Private m_lPos As Long

Public Property Get TheString() As String
   TheString = m_sString
End Property
Public Property Let TheString(ByRef sString As String)
   m_sString = sString
   m_lStrLen = Len(sString)
   Reset
End Property
Public Property Get Splitter(Optional ByVal iPos As Long = 1) As String
   Splitter = m_sSplitter(iPos)
End Property
Public Property Let Splitter(Optional ByVal iPos As Long = 1, ByVal sSplitter
 As String)
   If iPos > m_lSplitterCount Then
      m_lSplitterCount = iPos
      ReDim Preserve m_sSplitter(1 To m_lSplitterCount) As String
      ReDim Preserve m_lSplitterLen(1 To m_lSplitterCount) As Long
   End If
   m_sSplitter(iPos) = sSplitter
   m_lSplitterLen(iPos) = Len(sSplitter)
   Reset
End Property
Public Sub ClearSplitters()
   m_lSplitterCount = 0
   Erase m_sSplitter
End Sub
Public Sub Reset()
   m_lLastPos = 1
   m_lPos = 1
End Sub
Public Property Get NextItem() As String
   If m_lLastPos > 0 Then
      m_lPos = MinimumSplitPos()
      If m_lPos > 0 Then
         ' Middle item:
         NextItem = Mid$(m_sString, m_lLastPos, m_lPos - m_lLastPos)
         m_lLastPos = m_lPos + m_lSplitterLen(m_iThisSplitter)
      Else
         ' End
         NextItem = Mid$(m_sString, m_lLastPos)
         m_lLastPos = 0
      End If
   End If
End Property
Public Property Get SplitItem() As Long
   SplitItem = m_iThisSplitter
End Property
Private Function MinimumSplitPos() As Long
Dim lMinSplitPos As Long
Dim i As Long
Dim lPos As Long
   m_iThisSplitter = 0
   lMinSplitPos = m_lStrLen
   For i = 1 To m_lSplitterCount
      If m_lSplitterLen(i) > 0 Then
         lPos = InStr(m_lLastPos, m_sString, m_sSplitter(i))
         If lPos > 0 Then
            If lPos < lMinSplitPos Then
               lMinSplitPos = lPos
               m_iThisSplitter = i
            End If
         End If
      End If
   Next i
   If m_iThisSplitter = 0 Then
      MinimumSplitPos = 0
   Else
      MinimumSplitPos = lMinSplitPos
   End If
End Function
Public Property Get More() As Boolean
   More = (m_lLastPos > 0)
End Property

Private Sub Class_Initialize()
   'Debug.Print "cSplitString:Initialize"
End Sub

Private Sub Class_Terminate()
   'Debug.Print "cSplitString:Terminate"
End Sub