General |
6.52 Estrarre l'n-mo elemento (token) da un elenco delimitato. |
Alessandro Cara |
Public Function Token(str As String, str1 As String, num As Integer) As String ' str = stringa da controllare ' str1 = lista dei delimitatori ' num = numero del token da prelevare ' by Pia Toro & A. Cara Dim i As Long Dim k As Long Dim n As Long Dim j As Long Dim str0 As String Token = "NON TROVATO!" k = 0 str0 = Left(str1, 1) & str & Left(str1, 1) For i = 1 To Len(str0) If InStr(str1, Mid(str0, i, 1)) > 0 Then If InStr(str1, Mid(str0, i + 1, 1)) = 0 Or i = Len(str0) Then k = k + 1 If k = Num + 1 Then Token = Mid(str0, j + 1, n - j) Exit For Else j = i End If End If Else n = i End If Next i End Function Public Function NumToken(str As String, str1 As String) As Integer 'restituisce quanti elementi ci sono separati da uno o 'piu' separatori all'interno di una stringa ' str = stringa da controllare ' str1 = lista dei delimitatori ' by P.Toro & A.Cara Dim str0 As String Dim i As Integer Dim k As Integer str0 = Left(str1, 1) & str & Left(str1, 1) k = 0 For i = 1 To Len(str0) If InStr(str1, Mid(str0, i, 1)) > 0 Then If InStr(str1, Mid(str0, i + 1, 1)) = 0 Then k = k + 1 End If End If Next i NumToken = k End FunctionEsempio di utilizzo per la funzione Token: Token("pippo;topolino-minnie gastone", "; -", 1) -> pippo Token("pippo;topolino-minnie gastone", "; -", 2) -> topolino Token("pippo;topolino-minnie gastone", "; -", 3) -> minnie Token("pippo;topolino-minnie gastone", "; -", 4) -> gastone Esempio di utilizzo per la funzione NumToken: NumToken("pippo;topolino-minnie gastone", "; -") -> 4 |