?
Solved

Replace a character in text string with another character

Posted on 2008-11-13
9
Medium Priority
?
264 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:mgmhicks
  • 4
  • 4
9 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22954686
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

0
 

Author Comment

by:mgmhicks
ID: 22954720
Ok, almost there, but I want to replace back to the streamreader and write it to the file, replace what was there.
thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22954863
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 22954882
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.
0
 

Author Comment

by:mgmhicks
ID: 22954926
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.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22955049
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

0
 

Author Comment

by:mgmhicks
ID: 22955082
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?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 22955214
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.
0
 

Author Closing Comment

by:mgmhicks
ID: 31516574
You are the man!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question