Solved

Find and replace strings in txt file

Posted on 2011-02-21
3
522 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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 …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now