我目前正在做一个Soundex系统来做作业。对于作业,我需要将某些字母替换为某些数字。对于作业,我使用了do循环。问题在于字符串中只有一个字母被数字替换,而除第一个字母之外的其余字母都被删除。例如,Robert应该以“ R163”的形式出现,而是以“ R300”的形式出现。我想知道我在做什么错。我的代码是:
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim word As String = txtInput.Text
Dim first As String = txtInput.Text.Substring(0, 1)
Dim rest As String = txtInput.Text.Substring(1, word.Length - 1)
Dim test As String = ""
Dim combine As String = ""
Dim i As Integer = 0
Do
Select Case rest.Substring(i)
Case "a", "e", "i", "o", "u", "h", "y", "w"
test &= ""
Case "b", "f", "p", "v"
test &= "1"
Case "c", "g", "j", "k", "q", "s", "x", "z"
test &= "2"
Case "d", "t"
test &= "3"
Case "l"
test &= "4"
Case "m", "n"
test &= "5"
Case "r"
test &= "6"
End Select
i += 1
Loop Until i > rest.Length
combine = first & test
If combine.Length < 4 Then
Do While combine.Length < 4
combine &= "0"
Loop
ElseIf combine.Length > 4 Then
combine = combine.Substring(4)
End If
txtSound.Text = combine
End Sub
答案 0 :(得分:0)
试图使事情与您的示例相似...这应该起作用。
Dim word As String = txtInput.Text
Dim test As String = String.Empty
Dim combine As String = String.Empty
For i As Integer = 1 To word.Length - 1
Select Case word.Substring(i, 1)
Case "a", "e", "i", "o", "u", "h", "y", "w"
test &= ""
Case "b", "f", "p", "v"
test &= "1"
Case "c", "g", "j", "k", "q", "s", "x", "z"
test &= "2"
Case "d", "t"
test &= "3"
Case "l"
test &= "4"
Case "m", "n"
test &= "5"
Case "r"
test &= "6"
End Select
Next
combine = (word.Substring(0, 1) & test).PadRight(4, "0")
txtSound.Text = combine