Solved

Find and replace strings in txt file

Posted on 2011-02-21
3
524 Views
Last Modified: 2012-05-11
Hi Experts,

I would like to find and replace some strings in a text file and then re-write it again. I have a text file called test.txt:
A,B,C
B,B,A
A,C,C

And I want to load that file and then replace them as fallow:
A=1
B=2
C=3

So, the final result in the test.txt file should be like this:
1,2,3
2,2,1
1,3,3
As you can see from above, each line has different elements divided by a comma (,)

Note: I am using vb.net 2008
Thanks in advance
Regards
0
Comment
Question by:Sat80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 250 total points
ID: 34944087
try this

Dim MyContents = File.ReadAllLines("C:\test.txt")
        Dim sBuild As New StringBuilder
        For Each x In MyContents
            sBuild.Append(x.Replace("A", "1").Replace("B", "2").Replace("C", "3") & vbCrLf)
        Next
        File.WriteAllText("C:\output.txt", sBuild.ToString)

Open in new window

0
 
LVL 11

Assisted Solution

by:srikanthreddyn143
srikanthreddyn143 earned 250 total points
ID: 34944094
Try this piece of code -- "C:\TestData.txt" --> path of your text file

      Dim objStreamRdr As New StreamReader("C:\TestData.txt")

        Dim strTest As String = objStreamRdr.ReadToEnd()
        strTest = strTest.Replace("A", "1").Replace("B", "2").Replace("C", "3")
        objStreamRdr.Close()
        Dim objstreamWriter As New StreamWriter("C:\TestData.txt")
        objstreamWriter.Write(strTest)
        objstreamWriter.Close()


0
 

Author Closing Comment

by:Sat80
ID: 34944664
Thank you all
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 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