2010-07-14 8 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)) 
관련 문제