在不使用库函数的情况下在VB中查找子串的字符串

时间:2013-04-09 17:04:12

标签: vb.net

我对这个程序有点困惑。 我是Visual Basic的新手但是C的中间人。 其实我想在不使用Visual Basic的库函数的情况下获取字符串的子串。 这是C源代码我也给了我的VB代码。 1.本程序将获得用户的两个输入,即A&乙 2.比从B中查找子串。 3.最后打印结果。

int i,j=0,k=0,substr=0;
            for(i=0;i<strlen(a);i++)
            {

                if(a[i]==b[j])
                {
                    j++;

                    if(b[j]==0)
                    {
                        printf("second string is substring of first one");
                        substr=1;
                        break;
                    }
                }
            }
            for(i=0;i<strlen(b);i++)
            {
                if(b[i]==a[k])
                {
                    k++;
                    if(a[k]==0)
                    {
                        printf(" first string  is substring of second string");
                        substr=1;
                        break ;
                    }
                }
            }
            if(substr==0)
            {
                         printf("no substring present");
             }

虽然我的代码是

        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  

3 个答案:

答案 0 :(得分:2)

你的一些困惑是.Net字符串不仅仅是字符缓冲区。我假设你至少可以使用字符串。如果不能,则需要使用声明字符数组。除此之外,这应该是1:1的翻译:

Private Shared Function search(ByVal a As String, ByVal b As String) As Integer
    Dim i As Integer = 0
    Dim j As Integer = 0
    Dim firstOcc As Integer

    While i < a.Length

        While a.Chars(i)<>b.Chars(0) AndAlso i < a.Length
            i += 1
        End While

        If i >= a.Length Then Return -1 'search can not continue

        firstOcc = i

        While a.Chars(i)=b.Chars(j) AndAlso i < a.Length AndAlso j < b.Length
            i += 1
            j += 1
        End While

        If j = b.Length Then Return firstOcc
        If i = a.Length Then Return -1

        i = firstOcc + 1
        j = 0
    End While
    Return 0
End Function

Shared Sub Main() As Integer
    Dim a As String
    Dim b As String
    Dim loc As Integer

    Console.Write("Enter the main string :")
    a = Console.ReadLine()

    Console.Write("Enter the search string :")
    b = Console.ReadLine()

    loc = search(a, b)

    If loc = -1 Then
      Console.WriteLine("Not found")
    Else
      Console.WriteLine("Found at location {0:D}",loc+1)
    End If 

    Console.ReadKey(True)
End Sub

但请不要实际使用它。所有你真正需要的是:

Private Shared Function search(ByVal haystack as String, ByVal needle As String) As Integer
     Return haystack.IndexOf(needle)
End Function

答案 1 :(得分:1)

VB有一个名为InStr的内置函数,它是该语言的一部分。它返回一个整数,指定第一次出现在另一个字符串中的起始位置。

http://msdn.microsoft.com/en-us/library/8460tsh1(v=VS.80).aspx

皮特

答案 2 :(得分:1)

enter image description here

尝试这个,这将返回一个List(Of Integer),它包含指定搜索起始位置之后源文本中查找文本的所有匹配项的索引。

Option Strict On
Public Class Form1
''' <summary>
''' Returns an array of indexes where the find text occurred in the source text.
''' </summary>
''' <param name="Source">The text you are searching.</param>
''' <param name="Find">The text you are searching for.</param>
''' <param name="StartIndex"></param>
''' <returns>Returns an array of indexes where the find text occurred in the source text.</returns>
''' <remarks></remarks>
Function FindInString(Source As String, Find As String, StartIndex As Integer) As List(Of Integer)
    If StartIndex > Source.Length - Find.Length Then Return New List(Of Integer)
    If StartIndex < 0 Then Return New List(Of Integer)
    If Find.Length > Source.Length Then Return New List(Of Integer)
    Dim Results As New List(Of Integer)
    For I = StartIndex To (Source.Length) - Find.Length
        Dim TestString As String = String.Empty
        For II = I To I + Find.Length - 1
            TestString = TestString & Source(II)
        Next
        If TestString = Find Then Results.Add(I)
    Next
    Return Results
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim Search As String = "Hello world, this world is an interesting world"
        Dim Find As String = "world"
        Dim Indexes As List(Of Integer) = New List(Of Integer)
        Try
            Indexes = FindInString(Search, Find, 0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        RichTextBox1.Text = "Search:" & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & Search & vbCrLf & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & "Find:" & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & Find & vbCrLf & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & "-----------" & vbCrLf
        RichTextBox1.Text = RichTextBox1.Text & "Result Indexes:" & vbCrLf & vbCrLf
        For Each i As Integer In Indexes
            RichTextBox1.Text = RichTextBox1.Text & i.ToString & vbCr
        Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
End Class

这是另一种不使用.Net功能的方式。

Function FindInString(Source As String, Find As String, StartIndex As Integer) As Integer()
    If StartIndex > Len(Source) - Len(Find) Then Return {}
    If StartIndex < 0 Then Return {}
    If Len(Find) > Len(Source) Then Return {}
    Dim Results As Integer() = {}, ResultCount As Integer = -1
    For I = StartIndex To Len(Source) - Len(Find)
        Dim TestString As String = ""
        For II = I To I + Len(Find) - 1
            TestString = TestString & Source(II)
        Next
        If TestString = Find Then
            ResultCount += 1
            ReDim Preserve Results(ResultCount)
            Results(ResultCount) = I
        End If
    Next
    Return Results
End Function