2010-07-14 3 views
0

J'ai un tableau contenant a-z. Ensuite, j'ai une zone de texte et lorsque vous cliquez sur le bouton, il va remplacer le texte à l'intérieur de la zone de texte pour le numéro d'index du tableau. Exemple, de "abc" deviendra "0 1 2" Le code ci-dessous fait le travail. Puis-je savoir comment faire pour que je puisse remplacer le texte à l'intérieur de la zone de texte de "0 1 2" à "abc" basé sur le tableau? MerciRemplacement d'une chaîne par des données de tableau basées sur l'index de la matrice

Dim txtKey As String = readKeyTxt.Text 
    readKeyTxt.Text = "" 

    For Each b As String In txtKey 
     If chars.Contains(b) Then 
      Dim ab As Integer = Array.IndexOf(chars, b) 
      b = Replace(LCase(b), b, ab & " ") 

      readKeyTxt.Text &= b 
     End If 
    Next 

Répondre

0

Voici un exemple de code qui fera ce que vous avez décrit. Mais j'ai ce sentiment étrange que ce sont des devoirs.

Imports System 
Imports System.Text 

Module Module1 

    Sub Main() 
     ' I don't really care how you get your chars... but if they aren't all there they 
     ' will be lost in the conversion... 
     Dim lstChars As New List(Of Char) 
     For i As Integer = AscW("A"c) To AscW("z") 
     lstChars.Add(ChrW(i)) 
     Next 
     lstChars.Add(" "c) 
     lstChars.Add("."c) 
     Dim chars() As Char = lstChars.ToArray() 

     Dim testString As String = "The Quick Brown Fox Jumped Over The Lazy Dog." 
     Dim converted1 As String = ConvertStringToIndexes(testString, chars) 
     Dim converted2 As String = ConvertIndexesToString(converted1, chars) 

     Console.WriteLine(testString) 
     Console.WriteLine(converted1) 
     Console.WriteLine(converted2) 

     Console.ReadKey(True) 
    End Sub 

    Private Function ConvertStringToIndexes(ByVal s As String, ByVal chars() As Char) As String 
     Dim result As New StringBuilder() 
     Dim firstPass As Boolean = True 
     For Each c As Char In s.ToCharArray() 
     Dim idx = Array.IndexOf(chars, c) 
     If idx >= 0 Then 
      If firstPass Then 
       firstPass = False 
      Else 
       result.Append(" ") 
      End If 
      result.Append(idx) 
     End If 
     Next 
     Return result.ToString() 
    End Function 

    Private Function ConvertIndexesToString(ByVal s As String, ByVal chars() As Char) As String 
     Dim indexes() As String = s.Split(" "c) 
     Dim result As New StringBuilder() 

     For Each item As String In indexes 
     Dim idx As Integer = 0 
     If Int32.TryParse(item, idx) AndAlso chars.Length > idx Then 
      result.Append(chars(idx)) 
     End If 
     Next 
     Return result.ToString() 
    End Function 

End Module 
0

merci pour votre aide. Oui, c'est un devoir et j'ai réussi à le résoudre en utilisant d'autres méthodes. Voici le code.

Dim charList As New List(Of String) 

    For Each line In IO.File.ReadAllLines(Form1.broFreTxt.Text, System.Text.Encoding.Default) 
     charList.Add(line(1)) 
    Next 

    Dim chars = charList.ToArray() 
    Dim rslt As New List(Of String) 
    Dim data1() As String = outSubtracTxt.Text.Split(" ") 

    For Each b As String In data1 

     b = Replace(LCase(b), b, chars(b) & " ") 
     rslt.Add(b) 

    Next 

    Dim numbersAsString() As String = Array.ConvertAll(rslt.ToArray, New Converter(Of String, String)(Function(i) i.ToString)) 
Questions connexes