Dim maListe() As Integer = {1, 9, 63, 64, 65, 66, 67, 68, 69, 70, 241, 242, 243, 253, 254, 333} Dim mStart As Integer = 0 Dim mLast As Integer = 0 Dim sResult As String = "" mStart = maListe(LBound(maListe)) mLast = mStart sResult = mStart.ToString For r As Short = (LBound(maListe) + 1) To UBound(maListe) If maListe(r) = mLast + 1 Then ' La valeur suivante suit numéiquement la précédente ' Mémorise mLast = maListe(r) Else ' Pas de suite : on va écrire la chaine If mStart = mLast Then ' Il n'y a pas eu de suite numérique sResult += ";" & maListe(r).ToString Else ' Il y a eu une suite numérique sResult += "-" & mLast.ToString & ";" & maListe(r).ToString End If ' Mémorise mLast = maListe(r) mStart = mLast End If Next ' Cas des suites numériques en fin de liste If mStart <> mLast Then ' Il y a eu une suite numérique sResult += "-" & mLast.ToString End If Debug.Print(sResult)
Dim maListe() As Integer = {1, 9, 63, 64, 65, 66, 67, 68, 69, 70, 241, 242, 243, 254} Dim mStart As Integer = 0 Dim mLast As Integer = 0 Dim sResult As String = "" mStart = maListe(LBound(maListe)) mLast = mStart sResult = mStart.ToString For r As Short = (LBound(maListe) + 1) To UBound(maListe) If maListe(r) = mLast + 1 Then ' La valeur suivante suit numéiquement la précédente ' Mémorise mLast = maListe(r) Else ' Pas de suite : on va écrire la chaine If mStart = mLast Then ' Il n'y a pas eu de suite numérique sResult += ";" & maListe(r).ToString ' Mémorise mLast = maListe(r) mStart = mLast Else ' Il y a eu une suite numérique sResult += "-" & mLast.ToString & ";" & maListe(r).ToString ' Mémorise mLast = maListe(r) mStart = mLast End If End If Next Debug.Print(sResult)