0

1.以下程序将从用户那里获得两个输入,即 A 和 B
2.然后从 B 中找到子字符串
。3.最后打印结果。

虽然我的代码是

    Dim a As String
    Dim b As String
    a = InputBox("Enter First String", a)
    b = InputBox("Enter 2nd String", b)
        Dim i As Integer
        Dim j As Integer = 0
        Dim k As Integer = 0
        Dim substr As Integer = 0
        For i = 0 To a.Length - 1

            If a(i) = b(j) Then
                j += 1

                If b(j) = 0 Then
                MsgBox("second string is substring of first one")
                    substr = 1
                    Exit For
                End If
            End If
        Next i
        For i = 0 To b.Length - 1
            If b(i) = a(k) Then
                k += 1
                If a(k) = 0 Then
                MsgBox(" first string  is substring of second string")
                    substr = 1
                    Exit For
                End If
            End If
        Next i
        If substr = 0 Then
        MsgBox("no substring present")
        End If
End Sub

编译时会出现以下调试错误。

                                           Line Col
Error 1 Operator '=' is not defined for types 'Char' and 'Integer'. 17  24  
Error 2 Operator '=' is not defined for types 'Char' and 'Integer'. 27  24  
4

1 回答 1

0

这是因为这些行:

If b(j) = 0 Then

If a(k) = 0 Then

a(k)两者b(j)都是 Char 数据类型(将字符串视为字符数组),但您试图将它们与 int (0) 进行比较。

如果您正在寻找子字符串并且您正在使用 VB.NET,您可以尝试使用该IndexOf方法,例如:

If a.IndexOf(b) > -1 Then
    MsgBox("b is a substring of a")
ElseIf b.IndexOf(a) > -1 Then
    MsgBox("a is a substring of b")     
Else
    MsgBox("No substring found")
End

如果您使用的是 VBA,您也可以使用InStr,但我认为字符串是 1-indexed 而不是 0-indexed(因为它们在 VB.NET 中)所以您的检查可能类似于:

If InStr(a,b) > 0 Then
    MsgBox("b is a substring of a")
ElseIf InStr(b,a) > 0 Then
    MsgBox("a is a substring of b")     
Else
    MsgBox("No substring found")
End
于 2013-04-10T16:54:18.247 回答