vb.net – 在字符串中查找重复序列

我想在VB.Net中的字符串中找到重复序列,如:

昏暗测试为String =“EDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGBEDCRFVTGB”

我希望程序检测重复序列,以防EDCRFVTGB并计算它重复的次数.我的问题是找到字符串中的重复序列,我搜索了几种方法来做它但我没有得到解决方案,我尝试了快速排序算法,重复算法,但其中一些不能用于字符串.

我虽然创建子串并检查它们是否存在于字符串中,但我不知道如何获取子串,因为字符串上没有模式,字符串中也可能没有重复序列.

解决方法

首先检查目标字符串的一半是否重复两次.如果没有,检查字符串的三分之一是否重复三次.如果没有,检查字符串的四分之一是否重复四次.这样做直到找到匹配的序列.跳过商数不是整数的任何除数,使其表现更好.此代码应该可以解决这个问题并填写此描述无法澄清的任何空白:

Public Function DetermineSequence(ByVal strTarget As String) As String

    Dim strSequence As String = String.Empty

    Dim intLengthOfTarget As Integer = strTarget.Length

    'Check for a valid Target string.
    If intLengthOfTarget > 2 Then

        'Try 1/2 of Target,1/3 of Target,1/4 of Target,etc until sequence is found.
        Dim intCursor As Integer = 2

        Do Until strSequence.Length > 0 OrElse intCursor = intLengthOfTarget

            'Don't even test the string if its length is not a divisor (to an Integer) of the length of the target String.
            If IsDividendDivisibleByDivisor(strTarget.Length,intCursor) Then

                'Get the possible sequence.
                Dim strPossibleSequence As String = strTarget.Substring(0,(intLengthOfTarget / intCursor))

                'See if this possible sequence actually is the repeated String.
                If IsPossibleSequenceRepeatedThroughoutTarget(strPossibleSequence,strTarget) Then

                    'The repeated sequence has been found.
                    strSequence = strPossibleSequence

                End If

            End If

            intCursor += 1

        Loop

    End If

    Return strSequence

End Function

Private Function IsDividendDivisibleByDivisor(ByVal intDividend As Integer,ByVal intDivisor As Integer) As Boolean

    Dim bolDividendIsDivisbleByDivisor As Boolean = False

    Dim intOutput As Integer

    If Integer.TryParse((intDividend / intDivisor),intOutput) Then

        bolDividendIsDivisbleByDivisor = True

    End If

    Return bolDividendIsDivisbleByDivisor

End Function

Private Function IsPossibleSequenceRepeatedThroughoutTarget(ByVal strPossibleSequence As String,ByVal strTarget As String) As Boolean

    Dim bolPossibleSequenceIsRepeatedThroughoutTarget As Boolean = False

    Dim intLengthOfTarget As Integer = strTarget.Length
    Dim intLengthOfPossibleSequence As Integer = strPossibleSequence.Length

    Dim bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated As Boolean = False

    Dim intCursor As Integer = 1

    Do Until (intCursor * intLengthOfPossibleSequence) = strTarget.Length OrElse bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated

        If strTarget.Substring((intCursor * intLengthOfPossibleSequence),intLengthOfPossibleSequence) <> strPossibleSequence Then

            bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated = True

        End If

        intCursor += 1

    Loop

    If Not bolIndicatorThatPossibleSequenceIsCertainlyNotRepeated Then

        bolPossibleSequenceIsRepeatedThroughoutTarget = True

    End If

    Return bolPossibleSequenceIsRepeatedThroughoutTarget

End Function

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...