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)?
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):