Replace a character in text string with another character

I have a text file loader as streamreader.  I need to know how to find the 25 character on the first line and replace that character with another character.  Sample code would be great.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
This code replaces the 25th character with a "?".

        Dim s As String = "abcdefghijklmnopqrstuvwxyz"
        s = s.Substring(0, 24) & "?" & s.Substring(25)

Open in new window

mgmhicksAuthor Commented:
Ok, almost there, but I want to replace back to the streamreader and write it to the file, replace what was there.
Bob LearnedCommented:
StringBuilder example:

        Dim s1 As String = "The quick brown fox jumped over the lazy dog"
        Dim s2 As String = ReplaceAt(s1, "brown fox", "gray fox")
    Public Shared Function ReplaceAt(ByVal s As String, ByVal oldValue As String, ByVal newValue As String) As String
        Dim sb As New StringBuilder(s)
        sb.Replace(oldValue, newValue, s.IndexOf(oldValue), oldValue.Length)
        Return sb.ToString()
    End Function

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Bob LearnedCommented:
Ok, so that's what you get when you don't refresh first.  You can use a StreamReader to write to a file.  You would need a StreamWriter, and you can't have a StreamWriter against the same file, since it would be opened by the StreamReader.
mgmhicksAuthor Commented:
Ok, maybe I'm not getting it, but I do appreciate all the help.  This is what I have.

Dim FileContents As String = String.Empty
        Dim sFile As String
        Dim test As String
        sFile = LBox1.Items.Item(i)

        Dim sr As StreamReader = New StreamReader("\\Mgmdomain\Shared\Accounting\EFT Files" & "\" & sFile)
        FileContents = sr.ReadToEnd
        test = sr.ReadLine
        lblTest.text = test
(OK, I need to read the first line of the streamreader object, find the 15th character on the line and write back a value to that 15 character and save the file and do the next one the same way.)

thanks again.

Bob LearnedCommented:
StreamReader doesn't have any write methods, so it can't be used.

StreamWriter has write methods, but if you try to open a StreamWriter with the same file name, it will fail with an exception that the file is opened by another process.

I would suggest a change to achieve this goal:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReplaceInFile("c:\temp\Refunds.csv", "~", 0, 15, 1)
    End Sub
    Public Shared Sub ReplaceInFile(ByVal fileName As String, ByVal newValue As String, ByVal lineNumber As Integer, ByVal startIndex As Integer, ByVal length As Integer)
        Dim worker As New RichTextBox()
        worker.LoadFile(fileName, RichTextBoxStreamType.PlainText)
        Dim lines As String() = worker.Lines
        lines(lineNumber) = ReplaceAt(lines(lineNumber), newValue, startIndex, length)
        worker.Lines = lines
        worker.SaveFile(fileName, RichTextBoxStreamType.PlainText)
    End Sub
    Public Shared Function ReplaceAt(ByVal s As String, ByVal newValue As String, ByVal startIndex As Integer, ByVal length As Integer) As String
        Dim sb As New StringBuilder(s)
        sb.Remove(startIndex, length)
        sb.Insert(startIndex, newValue, newValue.Length)
        Return sb.ToString()
    End Function

Open in new window

mgmhicksAuthor Commented:
Do I need to even open a streamreader if I know the file name, the location of the character and the character I want to replace it with?
Bob LearnedCommented:
If you will notice, I used a RichTextBox as a worker, instead of the StreamReader, since it can read the file, and write back to the same file after processing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mgmhicksAuthor Commented:
You are the man!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.