Matthew Ian Clarke
Matthew Ian Clarke

Reputation: 37

Replacing numbers in a string vb.net

I am trying to a failing miserably to do a simple task. add a few numbers to textbox and then remove the current numbers at the end of the string. Essentially just replace numbers in the string My button click will only replace the text but then add the number to the original number Any help is very very welcome

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim myStreamReaderL1 As System.IO.StreamReader
    Dim myStream As System.IO.StreamWriter

    Dim myStr As String
    myStreamReaderL1 = System.IO.File.OpenText("C:\Test.txt")
    myStr = myStreamReaderL1.ReadToEnd()
    myStreamReaderL1.Close()

    myStr = myStr.Replace("Pin Number ", "Pin Number " & TextBox1.Text)
    myStream = System.IO.File.CreateText("C:\Test.txt")
    myStream.WriteLine(myStr)
    myStream.Close()

End Sub
End Class

Upvotes: 0

Views: 254

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

OK, let's see if I can help out a bit..

Your question should have been asked a bit more like:

I have a file c:\temp\a.txt that looks like this:

Name: John Smith
Pin Number: secret

and I want to replace the "secret" with a number thjat the user types into textbox 1, and then save the file again. I wrote this code:

Public Class Form1
...

but i cant make it replace the text properly

And I'd have said:

Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim txt as String = System.IO.File.ReadAllText("C:\temp\a.txt")
    txt = txt.Replace("Pin Number: secret", "Pin Number " & TextBox1.Text)
    System.IO.File.WriteAllText(txt, "C:\temp\a.txt")

  End Sub
End Class

Now, that's not much more complex than what you had (simpler, in fact)

How about, the text file contains an existing PIN that you don't know/can't hard code as "secret" and you want to replace it with another PIN?

Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim lines as String() = System.IO.File.ReadAllLines("C:\temp\a.txt")
    For i as Integer = 0 to lines.Length - 1
      If lines(i).StartsWith("Pin Number:") Then
        lines(i) = "Pin Number: " & Textbox1.Text 
      End If
    Next i
    System.IO.File.WriteAllLines(lines, "C:\temp\a.txt")

  End Sub
End Class

Here we ask for the file as an array of strings repenting the lines, so we can edit individual lines and then write it back. We loop through it looking for the pin number line, then simply replace that line with a whole new line containing the updated number, and write the file

Note that because this doesn't stop after it made the first replacement, if the file contains multiple lines of pins this will replace them all. Suppose you wanted to add another textbox where the user could type the name of the person whose PIN was to be replaced:

Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim replaceNext as Boolean = False
    Dim lines as String() = System.IO.File.ReadAllLines("C:\temp\a.txt")

    For i as Integer = 0 to lines.Length - 1
      If lines(i).EndsWith(NameTextBox.Text) Then
        replaceNext = true 'flip the boolean so we replace the next pin number we find
      Else If replaceNext AndAlso lines(i).StartsWith("Pin Number:") Then
        lines(i) = "Pin Number: " & Textbox1.Text 
        Exit For 'stop looking
      End If
    Next i
    System.IO.File.WriteAllLines(lines, "C:\temp\a.txt")

  End Sub
End Class

Upvotes: 5

Related Questions