Dim St As String Dim Str As String = "Str1;Val1-Str1;Val2-Str1;Val3|Str2;Val2-Str2;Val1|Str3;Val2" Dim SsTableau() As String Dim Tableau() As String = Split(Str, "|") Str = "" For i As Integer = 0 To UBound(Tableau) 'ListBox1.Items.Add(Tableau(i)) St = Tableau(i) SsTableau = Split(St, ";") St = SsTableau(0) & ":" & Replace(St, SsTableau(0), "") St = Replace(St, "-", "") St = Replace(St, ":;", ":") If i > 0 And i < (UBound(Tableau) + 1) Then Str &= "|" Str &= St Next MsgBox(Str)
Dim St As String Dim Str As String = "Str1;Val1-Str1;Val2-Str1;Val3|Str2;Val2-Str2;Val1|Str3;Val2" Dim SsTableau() As String Dim Tableau() As String = Str.Split("|"c) Str = "" For i As Integer = 0 To Tableau.Count - 1 'ListBox1.Items.Add(Tableau(i)) St = Tableau(i) SsTableau = St.Split(";"c) St = SsTableau(0) & ":" & St.Replace(SsTableau(0), "") St = St.Replace("-", "") St = St.Replace(":;", ":") If i > 0 And i < (Tableau.Count) Then Str &= "|" Str &= St Next
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question"Str1;Val1-Str1;Val3-Str2;Val2-Str2;Val1-Str3;Val2-Str1;Val2-Str2-Val4-Str1;Val5-Str1;Val1-Str1;Val3-Str2;Val2-Str1;Val15-Str2;Val1-Str3;Val2-Str1;Val2-Str2;ValX-Str3;ValY"
Str2-Val4
'Nous avons la chaine: Dim Stringona As String = _ "Str1;Val1-Str1;Val3-Str2;Val2-Str2;Val1-Str3;Val2-Str1;Val2-Str2-Val4-Str1;Val5" & _ "-Str1;Val1-Str1;Val3-Str2;Val2-Str1;Val15-Str2;Val1-Str3;Val2-Str1;Val2-Str2;ValX-Str3;ValY" 'On illimine tous les séparateurs et on les remplace par des espaces Stringona = Replace(Stringona, ";", " ") Stringona = Replace(Stringona, ":", " ") Stringona = Replace(Stringona, "-", " ") Stringona = Replace(Stringona, "|", " ") Dim Tableau() As String = Split(Stringona) 'Nous obtenons donc une liste de clés suivies de la valeur correspondante Dim ListeStr As New ListBox Dim t As Integer Dim Str As String Dim Existe As Boolean For i As Integer = 0 To UBound(Tableau) If UCase(Tableau(i).ToCharArray(0, 1)) = "S" Then 'Si ça commence par un 'S' (une clé donc) Existe = False For t = 0 To ListeStr.Items.Count - 1 If Tableau(i) ListeStr.Items.Item(t).ToString.ToCharArray(0, Tableau(i).Length) Then Existe True : Exit For Next For t = 0 To ListeStr.Items.Count - 1 Next If Existe = False Then ListeStr.Items.Add(Tableau(i) & ":") Else For t = 0 To ListeStr.Items.Count - 1 If Tableau(i - 1) = ListeStr.Items.Item(t).ToString.ToCharArray(0, Tableau(i).Length) Then Str = ListeStr.Items.Item(t).ToString & ";" & Tableau(i) ListeStr.Items(t) = Str Exit For End If Next End If Next Str = "" For i = 0 To ListeStr.Items.Count - 1 If i < ListeStr.Items.Count - 1 Then Str &= ListeStr.Items.Item(i).ToString.Replace(":;", ":") & "|" Else Str &= ListeStr.Items.Item(i).ToString.Replace(":;", ":") End If Next MsgBox(Str)
Dim Stringona As String = _ "Str1;Val1-Str1;Val3-Str2;Val2-Str2;Val1-Str3;Val2-Str1;Val2-Str2;Val4-Str1;Val5" & _ "-Str1;Val1-Str1;Val3-Str2;Val2-Str1;Val15-Str2;Val1-Str3;Val2-Str1;Val2-Str2;ValX-Str3;ValY" Dim Tableau() As String = Split(Stringona, "-") Dim TableauCleValeur() As String Dim ListeStr As New ListBox Dim Existe As Boolean Dim Str As String = "" Dim t As Integer For i As Integer = 0 To UBound(Tableau) Existe = False TableauCleValeur = Split(Tableau(i), ";") For t = 0 To ListeStr.Items.Count - 1 If TableauCleValeur(0) = ListeStr.Items.Item(t).ToString.ToCharArray(0, TableauCleValeur(0).Length) Then ListeStr.Items(t) &= ";" & TableauCleValeur(1) Existe = True Exit For End If Next If Existe = False Then ListeStr.Items.Add(TableauCleValeur(0) & ":" & TableauCleValeur(1)) Next For i = 0 To ListeStr.Items.Count - 1 If i < ListeStr.Items.Count - 1 Then Str &= ListeStr.Items.Item(i).ToString & "|" Else Str &= ListeStr.Items.Item(i).ToString End If Next MsgBox(Str)
Dim St As String Dim Str As String = "Str1;Val1-Str1;Val2-Str1;Val3|Str2;Val2-Str2;Val1|Str3;Val2" Dim SsTableau() As String Dim Tableau() As String = Str.Split("|"c) Str = "" For i As Integer = 0 To Tableau.Count - 1 'ListBox1.Items.Add(Tableau(i)) St = Tableau(i) SsTableau = St.Split(";"c) St = SsTableau(0) & ":" & St.Replace(SsTableau(0), "") St = St.Replace("-", "") St = St.Replace(":;", ":") If i > 0 And i < (Tableau.Count + 1) Then Str &= "|" Str &= St Next
ShayW: ....plutotDim St As String Dim Str As String = "Str1;Val1-Str1;Val2-Str1;Val3|Str2;Val2-Str2;Val1|Str3;Val2" Dim SsTableau() As String Dim Tableau() As String = Str.Split("|"c) Str = "" For i As Integer = 0 To Tableau.Count - 1 'ListBox1.Items.Add(Tableau(i)) St = Tableau(i) SsTableau = St.Split(";"c) St = SsTableau(0) & ":" & St.Replace(SsTableau(0), "") St = St.Replace("-", "") St = St.Replace(":;", ":") If i > 0 And i < (Tableau.Count) Then Str &= "|" Str &= St Next
Dim Stringona as String = "Str1;Val1-Str1;Val3-Str2;Val2-Str2;Val1-Str3;Val2-Str1;Val2-Str2-Val4-Str1;Val5"
va bien
NHenry:
Bonjour,
A la place de "ArrayList" utilises plutôt "List(Of T)" (où T est remplacé par le type désiré), ce sera encore plus sûr au niveau du type des valeurs dans la collection.
Ta méthode fonctionne
c'est de ma faute car je n'ai pas été précis au départ.
En réalité la chaine pourrait parvenir de façon désoordonnée du type
Comme il dit Shayw ça change tout (mais je m'en doutais un peu) j'ai voulu être bête et discipliné
c'est une façon de trier entre autres, maintenant que tu dis que ça peux arriver dans un ordre complètement différent, j'ai une question:
Tu as supprimé les "|" en tant que séparateur? ils n'existent plus?
Ce soir (chez moi, UTC-4) si j'ai un moment je vais le simplifier, par contre je dois savoir impérativement quels sont tous les séparateurs (:;-| etc).
Dim strDepart As String = "Str1;Val1-Str1;Val3-Str2;Val2-Str1;Val15-Str2;Val1-Str3;Val2-Str1;Val2-Str2;ValX-Str3;ValY" Dim strFinal As String
strFinal = "Str1:Val1;Val3;Val15;Val2|Str2:Val2;Val1;ValX|Str3:Val2;ValY"