从字符串中删除重复数字(即使 660 = 60)

Removing duplicate digit from string (i.e. make 660 = 60)

我在 Soundex 上有一个 vb.net 项目。对于那些不知道的人,Soundex 将您输入的单词导出为字母和数字。

例如:

单词:胡萝卜

输出:C663

我的任务是删除第二个数字,如果它们直接相邻并且是相同的数字。所以对于这个例子,C663 只需要输出为 C63。我似乎无法弄清楚。我试图使用 Distinct 子句,但我就是想不出来。

这是我的代码,如有任何帮助,我们将不胜感激。

Public Class Form1

Dim Word As String

Private Sub btnEncode_Click(sender As Object, e As EventArgs) Handles btnEncode.Click

    Word = txtInput.Text
    txtOutput.Text = Output(Word)

End Sub

Public Function Output(Word As String) As String

    Return Output(Word, 4)

End Function

Public Function Output(Word As String, Length As Integer) As String

    Dim returnValue As String = ""

    Dim Size As Integer = Word.Length

    If (Size > 1) Then

        Word = Word.ToUpper()

        Dim Chars() As Char = Word.ToCharArray()
        Dim i As Integer
        Dim wordSize As Integer = Size - 1
        Dim value As Integer
        Dim newString As New System.Text.StringBuilder

        newString.Append(Chars(0))

        For i = 1 To wordSize
            Select Case Chars(i)
                Case "A", "E", "I", "O", "U", "H", "W", "Y"
                    value = 0
                Case "B", "F", "P", "V"
                    value = 1
                Case "C", "G", "J", "K", "Q", "S", "X", "Z"
                    value = 2
                Case "D", "T"
                    value = 3
                Case "L"
                    value = 4
                Case "M", "N"
                    value = 5
                Case "R"
                    value = 6
            End Select

            If value <> 0 Then
                newString.Append(value)
            End If

        Next

    End If

    Return returnValue
End Function
End Class

使用循环检查每个字符,看它们是否彼此相邻且相同,如果是,则不要将该字符添加到新字符串中:

Public Function Output(Word As String, Length As Integer) As String

Dim returnValue As String = ""

Dim Size As Integer = Word.Length
'initialize temp string to first character of word
Dim newcondensedWord AS String = Word(0)
'Loop starting at index 1 or second character compare each character to previous during loop, if the same continue else append to variable newcondensedWord
FOR i AS Integer = 1 TO Word.Length - 1
     Dim temp as CHAR = Word(i - 1)
     IF Word(i) = Word(i - 1) THEN
          Continue
     ELSE
          newcondensedWord += Word(i)
     END IF
NEXT

'Now that we have removed the characters that are duplicates and adjacents set word equal to the condensed word
Word = newcondensedWord

只需检查当前循环中的角色。这是一个工作函数,演示了您需要做什么。

Public Function Output(Word As String, Length As Integer) As String

    Dim returnValue As String = String.Empty

    Dim Size As Integer = Word.Length

    If (Size > 1) Then

        Word = Word.ToUpper()

        Dim Chars() As Char = Word.ToUpper().ToCharArray()
        Dim value As Char
        Dim newString As New System.Text.StringBuilder

        newString.Append(Chars(0))

        For i = 1 To Size - 1
            Select Case Chars(i)
                Case "A", "E", "I", "O", "U", "H", "W", "Y"
                    value = "0"c
                Case "B", "F", "P", "V"
                    value = "1"c
                Case "C", "G", "J", "K", "Q", "S", "X", "Z"
                    value = "2"c
                Case "D", "T"
                    value = "3"c
                Case "L"
                    value = "4"c
                Case "M", "N"
                    value = "5"c
                Case "R"
                    value = "6"c
            End Select

            If value <> "0" Then
                If newString.Length > 0 AndAlso newString(newString.Length - 1) = value Then
                    Continue For
                End If
                newString.Append(value)
            End If

        Next

        returnValue = newString.ToString()

    End If

    Return returnValue
End Function