Link to home
Start Free TrialLog in
Avatar of mgmhicks
mgmhicks

asked on

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.
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

This code replaces the 25th character with a "?".

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

Open in new window

Avatar of mgmhicks
mgmhicks

ASKER

Ok, almost there, but I want to replace back to the streamreader and write it to the file, replace what was there.
thanks
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

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.
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)
        'rtFileData.Clear()

        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.

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are the man!!