0

1.The Following Program will get two inputs from user i.e A & B
2. Than Find the sub string from B.
3. Finally Print the result.

While my code is

    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

While compiling it gives following debugging errors.

                                           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  
2

1 Answer 1

0

It is because of these lines:

If b(j) = 0 Then

If a(k) = 0 Then

As a(k) and b(j) are both of the Char data type (think of the string as an array of characters) but you are trying to compare them to an int (0).

If you are looking for a substring and you're using VB.NET you could try using the IndexOf method, for a very naive example:

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

If you are using VBA, you could also use InStr but I think with this the string is 1-indexed instead of 0-indexed (as they are in VB.NET) so your check may look something like:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.