tags:

views:

18

answers:

2

Hi all, i have an array contains a-z. Then i have a textbox and when click on the button, it will replace the text inside the textbox to the index number of the array. Example, from "abc" will become "0 1 2" The code below do the job. May i know how to do so that i can replace the text inside the textbox from "0 1 2" back to "abc" based on the array? Thanks

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
A: 

Here's some sample code that will do what you described. But I have this strange feeling that this is homework.

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
mattmc3
A: 

Hi, thanks for helping. Yes, it is a homework and i managed to solve it using other method. Here is the 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))