title: Reading text from a file
body: <p>I am writing some code to search a file at the start of the file until a string is found.</p>
  
  <p>Here is the code that works:</p>
  
  <pre><code>    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 &gt;= 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 &lt; 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
  </code></pre>
  
  <p>I am wanting to edit thsi code such that I can choose a start position in the file to start searching.</p>
  
  <p>Here is the code that I am working on:</p>
  
  <pre><code>    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 &gt;= 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 &lt; 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
  </code></pre>
  
  <p>Can I please have some help to get this working?</p>
  
  <p><strong>FINAL CODE</strong></p>
  
  <pre><code>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 &gt;= 0
  
              integerCurrentPosition += 1
  
              tmpString = Convert.ToChar(reader.Read)
  
              If integerCurrentPosition &gt; integerStartPosition Then
                  sb.Append(tmpString)
                  previousLength = sb.Length
                  sb.Replace(searchString, String.Empty)
                  If sb.Length &lt; previousLength Then
                      result = sb.ToString()
                      Exit While
                  End If
              End If
  
          End While
      End Using
      Return result
  End Function
  </code></pre>