Advertisement
gunawantw

gwImplode and gwExplode

Sep 4th, 2019
3,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Dim tempStr As String
  2.     'Dim tempVar As Variant
  3.    
  4.     'tempstr = "gunawan~rini~faiz~rafi"
  5.     'tempvar = gwexplode(tempstr,"~")
  6.    
  7.     Dim tempvar(2)
  8.     tempvar(0) = "gunawan"
  9.     tempvar(1) = "faiz"
  10.     tempvar(2) = "rafi"
  11.    
  12.     tempstr = gwimplode(tempvar,":")
  13.    
  14.     Dim t2var As Variant
  15.     t2var = gwexplode(tempstr,":")
  16.    
  17.  
  18.  
  19.  
  20. Function gwImplode(v As Variant,sep As String) As String
  21.     Dim s As String
  22.     Dim i As Integer
  23.     s=""
  24.     For i=0 To UBound(v)
  25.         If i = UBound(v)  Then
  26.             s = s + v(i)
  27.         Else
  28.             s = s + v(i) + sep
  29.         End If
  30.     Next   
  31.     gwImplode = s
  32.  
  33. End Function
  34.  
  35.  
  36. Function gwexplode(textstr As String,sep As String) As Variant
  37. 'convert from string to variant
  38.     'gtw
  39.     Dim temptextstr As String
  40.     Dim tstr As String
  41.     Dim t1 As string
  42.    
  43.     temptextstr = textstr
  44.     If Right(temptextstr,1) <> sep Then
  45.         temptextstr = temptextstr +sep+" "
  46.     End If
  47.     Dim tvar() As Variant
  48.    
  49.     Dim j As Integer, i As Integer
  50.     j=-1
  51.     i=0
  52.    
  53.     For i=1 To Len(temptextstr)
  54.         If Mid(temptextstr,i,1) = sep Then
  55.             j=j+1
  56.         End If
  57.     Next
  58.    
  59.     ReDim Preserve tvar(j)
  60.     j=-1
  61.     i=0
  62.     tstr = temptextstr
  63.     While tstr <> ""
  64.         i=i+1
  65.         t1 = Mid(tstr,i,1)
  66.         If t1 = sep Then
  67.             j=j+1
  68.             tvar(j) = Trim(Mid(tstr,1,i-1))
  69.             tstr = Right(tstr,Len(tstr)-Instr(tstr,sep))
  70.             If tstr=" " Then tstr=""
  71.             'Msgbox tvar(j)
  72.             i=0
  73.         End If
  74.     Wend
  75.    
  76.     gwexplode = tvar
  77. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement