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

  • Home
  • SEARCH
  • 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 344005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:57:02+00:00 2026-05-12T10:57:02+00:00

I have a text file containing the data in following format 12345 Abdt3 hy45d

  • 0

I have a text file containing the data in following format

12345 Abdt3 hy45d et45a 76huj agsj7

Now I want to replace hy45d with another sting that is created on run time. But I am not finding any way to do it. It keep replacing the firt item or append it in next line.

Can any one plz help me resolve this issue

Code that I am using is

Public Sub SaveToTextFile(ByVal PNR As String, ByVal CaseCount As Integer, ByVal Type As String)


    '- Location of the Text files
    Dim targetFile As String = "C:\TestReport\TextFiles\PNR.txt"

    '- Open the target file/stream
    Dim sw As StreamWriter = New StreamWriter(targetFile, True)

    ' Try
    If (CaseCount = 0 And Type = 1) Then
        sw.Write(PNR, 0, 6)
    ElseIf (CaseCount = 1 And Type = 1) Then
        sw.WriteLine(PNR, 6, 7)
    ElseIf (CaseCount = 2 And Type = 1) Then
        sw.WriteLine(PNR, 13, 7)
    ElseIf (CaseCount = 3 And Type = 1) Then
        sw.WriteLine(PNR, 20, 7)
    ElseIf (CaseCount = 0 And Type = 2) Then
        sw.WriteLine(PNR, 27, 7)
    ElseIf (CaseCount = 1 And Type = 2) Then
        sw.WriteLine(PNR, 34, 7)
    ElseIf (CaseCount = 2 And Type = 2) Then
        sw.WriteLine(PNR, 41, 7)
    ElseIf (CaseCount = 3 And Type = 2) Then
        sw.WriteLine(PNR, 48, 7)
    ElseIf (CaseCount = 0 And Type = 3) Then
        sw.WriteLine(PNR, 55, 7)
    ElseIf (CaseCount = 1 And Type = 3) Then
        sw.WriteLine(PNR, 62, 7)
    ElseIf (CaseCount = 2 And Type = 3) Then
        sw.WriteLine(PNR, 69, 7)
    ElseIf (CaseCount = 3 And Type = 3) Then
        sw.WriteLine(PNR, 76, 7)
    End If

    'Catch ex As Exception
    '    Console.Write(ex.ToString())
    'End Try

    sw.Close()
End Sub
  • 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-05-12T10:57:02+00:00Added an answer on May 12, 2026 at 10:57 am

    There is no good way of doing this. Text files may use encodings that allocate more than one byte for a character. Simply put, even if you know the character position of the target string, you don’t (in general) know its byte position.

    The .NET classes therefore reasonably forbid random access on text files. The standard way of coping with this is to read the whole (or at least part) of the text file, change the string, and write the changed string back.

    So:

    Dim text = My.Computer.FileSystem.ReadAllText("File.txt")
    text = text.Replace("hy45d", "myfancynewstring")
    My.Computer.FileSystem.WriteAllText("File.txt", text, False)
    

    Short and sweet.

    Or, if you want to search by column:

    Dim text = My.Computer.FileSystem.ReadAllText("File.txt")
    Dim columns = text.Split(" "c)
    '' // Replace third column:
    columns(2) = "myfancynewstring"
    My.Computer.FileSystem.WriteAllText("File.txt", String.Join(" ", columns) , False)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file containing lines of data. I can use the following
I have a text file containing RGB data for an image, how can I
I have a text file containing the following content: 0 12 1 15 2
I have a big text file containing data that needs to be extracted and
I have an XML file containing some scientific text that I want to display
I have a text file containing data/fields which are separated by exact column no.
I have a text file containing data like this: This is just text -------------------------------
I have a text file containing tabular data. What I need to do is
I have a text file containing unwanted null characters (ASCII NUL, \0 ). When
I have a text file containing a number of records. Each record is stored

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.