Home > How-To Library > String Manipulation
How to return a substring between two delimiters in a string.
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 'A simple parse function which returns in an array all words between two delimiters. 'if you have a string like strText="this is 'a test ' in order to'test' 'the function syntaxe'll be 'rs=Delimiter(strText,"'","'") if you want to get everything between the "'" Dim rs() As String Private Sub Form_Load() Dim I As Integer Dim strText As String strText = "this is 'a test ' in order to'test'" rs = Delimiter(strText, "'", "'") For I = LBound(rs) To UBound(rs) Debug.Print rs(I) Next I End Sub Private Function Delimiter(ByVal texte As String, ByVal delimiter1 As String, ByVal delimiter2 As String) As Variant Dim interne() As String Dim cpt As Integer Dim Flag1 As Boolean Dim flag2 As Boolean ReDim Preserve interne(1) 'initialisation du tableau à 1 qte = 1 'le premier composant du tableau en sortie aura l'index 1 Do While cpt < Len(texte) cpt = cpt + 1 If Mid$(texte, cpt, 1) = delimiter1 And Not Flag1 Then Flag1 = True GoTo lenext End If If Mid$(texte, cpt, 1) = delimiter2 And Flag1 = True Then Flag1 = False flag2 = True qte = qte + 1 ReDim Preserve interne(qte + 1) GoTo lenext End If If Flag1 Then interne(qte) = interne(qte) & Mid$(texte, cpt, 1) lenext: Loop Delimiter = interne End Function
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.