从文件中读取文本

时间:2012-09-06 05:48:20

标签: vb.net string file visual-studio-2012

我正在编写一些代码来搜索文件开头的文件,直到找到一个字符串。

以下是有效的代码:

    Private Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String) As String
        Dim result As String = String.Empty
        Dim sb As New System.Text.StringBuilder()
        Dim previousLength As Integer = 0
        Using reader As New IO.StreamReader(txtFile)
            'Read the file 1 char at a time and append it to sb
            While reader.Peek >= 0
                sb.Append(Convert.ToChar(reader.Read))
                'store the current length of sb
                previousLength = sb.Length
                'attemp to replace the search string with an empty string. If the length changed after the replacement
                'then the following conditions must be true:
                ' 1. we have found the search string in sb.
                ' 2. the search string was at the end of sb.
                sb.Replace(searchString, String.Empty)
                If sb.Length < previousLength Then
                    'since we have found the search string,  exit the loop and return the string currently in sb.
                    result = sb.ToString()
                    Exit While
                End If
            End While
        End Using
        Return result
    End Function

我想编辑这些代码,以便我可以在文件中选择一个开始位置来开始搜索。

以下是我正在处理的代码:

    Public Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String, integerStartPosition As Integer) As String
    Dim result As String = String.Empty
    Dim sb As New System.Text.StringBuilder()
    Dim previousLength As Integer = 0
    Using reader As New IO.StreamReader(txtFile)
        'Read the file 1 char at a time and append it to sb
        While reader.Peek >= integerStartPosition
            sb.Append(Convert.ToChar(reader.Read))
            'store the current length of sb
            previousLength = sb.Length
            'attemp to replace the search string with an empty string. If the length changed after the replacement
            'then the following conditions must be true:
            ' 1. we have found the search string in sb.
            ' 2. the search string was at the end of sb.
            sb.Replace(searchString, String.Empty)
            If sb.Length < previousLength Then
                'since we have found the search string,  exit the loop and return the string currently in sb.
                result = sb.ToString()
                Exit While
            End If
        End While
    End Using
    Return result
End Function

我可以帮忙解决这个问题吗?

最终代码

Public Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String, integerStartPosition As Integer) As String
    Dim result As String = String.Empty
    Dim sb As New System.Text.StringBuilder()
    Dim tmpString As String = ""
    Dim previousLength As Integer = -1
    Dim integerCurrentPosition As Integer 
    Using reader As New IO.StreamReader(txtFile)
        While reader.Peek >= 0

            integerCurrentPosition += 1

            tmpString = Convert.ToChar(reader.Read)

            If integerCurrentPosition > integerStartPosition Then
                sb.Append(tmpString)
                previousLength = sb.Length
                sb.Replace(searchString, String.Empty)
                If sb.Length < previousLength Then
                    result = sb.ToString()
                    Exit While
                End If
            End If

        End While
    End Using
    Return result
End Function

1 个答案:

答案 0 :(得分:0)

您应该在while循环中放置位置检查。这是一个如何做到的简单示例。

Private Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String) As String
            Dim result As String = String.Empty
            Dim sb As New System.Text.StringBuilder()
            Dim previousLength As Integer = 0
            Dim currentPosition as Integer = 0

            Using reader As New IO.StreamReader(txtFile)
                'Read the file 1 char at a time and append it to sb
                While reader.Peek >= 0
                    If currentPosition < integerStartPosition Then
                       Continue While
                    End if 

                    sb.Append(Convert.ToChar(reader.Read))
                    'store the current length of sb
                    previousLength = sb.Length
                    'attemp to replace the search string with an empty string. If the length changed after the replacement
                    'then the following conditions must be true:
                    ' 1. we have found the search string in sb.
                    ' 2. the search string was at the end of sb.
                    sb.Replace(searchString, String.Empty)
                    If sb.Length < previousLength Then
                        'since we have found the search string,  exit the loop and return the string currently in sb.
                        result = sb.ToString()
                        Exit While
                    End If

                    currentPosition = currentPosition + 1
                End While
            End Using
            Return result
        End Function

更新:

或者您可以尝试StreamReader.Read Method (Char[], Int32, Int32)方法将流读入数组,指定起始位置。

  

从当前流中读取最多计数字符   缓冲区,从索引开始。

有关详细信息,请参阅教程

StreamReader

祝你好运!