title: Splitting but retaining delimiters
body: <p>I've been working on a function to split a string but retain its delimiters.</p>
  
  <pre><code>Function SplitButRetainDelims(ByVal text As String, ByVal delimiters As String) As String()
  
      Dim Arr() As String, currentDelim As String
      Dim i As Long
  
      For i = 1 To Len(delimiters)
          currentDelim = Mid$(delimiters, i, 1)
          Arr = Split(Replace(text, currentDelim, currentDelim &amp; "~"), "~")
      Next
  
      SplitButRetainDelims = Arr
  
  End Function
  </code></pre>
  
  <p>The problem is that the way I have it now, </p>
  
  <pre><code>Arr = Split(Replace(text, currentDelim, currentDelim &amp; "~"), "~")
  </code></pre>
  
  <p>will overwrite Array each time with only the split from the current delimiter. So instead of keeping the progress that previous iterations made, it resets.</p>
  
  <p>I've nearly gotten to an acceptable solution before, but it was too involved to offer much benefit.</p>
  
  <p>Edit: example</p>
  
  <pre><code>SplitButRetainDelims("potato (onion/soup / Green BEANS", " ()/") 
  = ("potato", " ", "(", "onion", "/", "soup", " ", "/", "Green", " ", "BEANS")
  </code></pre>