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 6176057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:01:59+00:00 2026-05-24T00:01:59+00:00

I need to replace certain characters in each line of a file. The file

  • 0

I need to replace certain characters in each line of a file. The file is not delimited, but each line has a fixed format. For example, I need to replace 5 questionmarks into ‘x’s. The 5 questionmarks that need to be replaced in each line are found in position 10. So for example:

Input file:

abdfg trr?????456
g?????dhs?????diu
eerrttyycdhjjhddd

The output file should be:

abdfg trrxxxxx456
g?????dhsxxxxxdiu
eerrttyycdhjjhddd

The output file will be saved as a different file to a specific location

What’s the best way to do this in VB.NET (I’m a little new to VB, so any code sample would help)?

  • 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-24T00:02:00+00:00Added an answer on May 24, 2026 at 12:02 am

    One possible solution is to parse each line in the file using a StreamReader (via the ReadLine() method). As you read in each line, you can use a StreamWriter to write the original line out (via the WriteLine(String) method), with one adjustment. If the line meets your replacement requirements, you will use the String.Replace(String, String) method to swap out the old string for the replacement string.

    Here is a solution (compiled and tested with your sample data above). You would still want to add some exception handling (at the very least, ensure the file exists first):

    Public Shared Sub ReplacementExample(string originalFile, string newFile)
    
        ' Read contents of "oringalFile"
        ' Replace 5 ? characters with 5 x characters.
        ' Write output to the "newFile"
        ' Note: Only do so if the ? characters begin at position 10
    
    
        Const replaceMe As String = "?????"
        Const replacement As String = "xxxxx"
    
        Dim line As String = Nothing
    
        Using r As New StreamReader(originalFile)
            Using w As New StreamWriter(newFile)
    
                line = r.ReadLine()
    
                While Not line Is Nothing
                    w.WriteLine(line.Substring(0, 9) + _
                                line.Substring(9).Replace(replaceMe, replacement))
    
                    line = r.ReadLine()
                End While
    
            End Using
        End Using
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace certain ascii characters like @ and & with their hex
I need to be able to replace certain characters so that I can use
I need to replace a string in certain documents. I have googled this code,
I need to replace some text in a JNLP file using a DOS batch
I need to replace accented characters in a string with their unaccented counterparts and
I need a PCRE expresssion (regex) to match and replace a certain attribute and
I have a file with binary data and I need to replace a few
What's best performing script to replace certain characters with a maximum of one other
I use python 2.5, i like to replace certain variables in a txt file
I have some large csv files (1.5gb each) where I need to replace specific

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.