2012-09-06 106 views
1

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

这里是工作的代码:

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 

我想编辑thsi代码,这样我可以选择在文件中的起始位置开始搜索。

这里是我的工作代码:

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 

回答

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 

UPDATE:

或者你可以尝试StreamReader.Read Method (Char[], Int32, Int32)方法读取流进阵列,并指定起始位置。

从索引开始,将当前流中最大数量的字符读入 缓冲区。

欲了解更多信息检查教程

StreamReader

祝您好运!

+0

peek方法实际上并不返回当前位置,它只是返回刚刚读取的字符的asc值。我寻找'寻求'的声明......但读者阶层没有。有没有办法计算peek()语句的当前位置? –

+0

哦,我的错。我无法找到获取当前位置的方法,但出于这个原因,您可以使用一个变量。我编辑了我的代码。 – hgulyan

+0

太棒了,ty。我修改了一下。我在我的问题结尾处发布了代码。 –