Solved

Find and replace strings in txt file

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now