2010-07-14 3 views
0

У меня есть массив, содержащий a-z. Тогда у меня есть текстовое поле, и при нажатии на кнопку он заменит текст внутри текстового поля на индексный номер массива. Пример, из «abc» станет «0 1 2» Приведенный ниже код выполняет эту работу. Могу ли я узнать, как сделать так, чтобы я мог заменить текст внутри текстового поля с «0 1 2» на «abc» на основе массива? СпасибоЗамена строки данными массива на основе индекса массива

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 

ответ

0

Вот пример кода, который будет делать то, что вы описали. Но у меня такое странное чувство, что это домашнее задание.

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

благодарит за помощь. Да, это домашняя работа, и мне удалось ее решить, используя другой метод. Вот код.

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)) 
Смежные вопросы