Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:22:29+00:00 2026-06-10T21:22:29+00:00

I am writing some code to search a file at the start of the

  • 0

I am writing some code to search a file at the start of the file until a string is found.

Here is the code that works:

    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

I am wanting to edit thsi code such that I can choose a start position in the file to start searching.

Here is the code that I am working on:

    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

Can I please have some help to get this working?

FINAL 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 >= 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:22:30+00:00Added an answer on June 10, 2026 at 9:22 pm

    You should put position checking inside the while loop. Here’s a simple example how to do it.

    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:

    Or you can try StreamReader.Read Method (Char[], Int32, Int32) method to read stream into array, specifying starting position.

    Reads a maximum of count characters from the current stream into
    buffer, beginning at index.

    For more info check tutorial

    StreamReader

    Good Luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some code that will go through a file, edit it as
I am writing some code that uses fixed regexs to search strings and pattern
I'm writing some code that will take a screenshot of another application, given its'
I'm writing some code (just for fun so far) in Python that will store
I'm writing some code that has a lot of substitution. There's a list<char> productions
I am writing some code that connects to a website, and using C#, and
I'm writing some code that iterates a set of POS tags (generated by pos_tag
Here Is My Code... Here i have one TextBox. When writing some text automatically
In writing some code today, I have happened upon a circumstance that has caused
I am writing some PHP code that's kind of like this: foreach($filter[1] as $reject){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.