Solved

Find and replace strings in txt file

Posted on 2011-02-21
3
492 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you all
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

10 Experts available now in Live!

Get 1:1 Help Now