?
Solved

Find and replace strings in txt file

Posted on 2011-02-21
3
Medium Priority
?
527 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 1000 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 1000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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