Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        Dim strFile As String

        With ofd
            .Title = "Select a List"
            .Filter = "Text Files|*.txt|All Files|*.*"
            .ShowDialog()
            strFile = .FileName
        End With

        If System.IO.File.Exists(strFile) = False Then Exit Sub

        Dim Reader As New System.IO.StreamReader(strFile)
        Dim strData As String
        Dim i As Integer
        Dim sp() As String

        strData = Reader.ReadToEnd()

        sp = strData.Split(vbCrLf)

        For i = 0 To UBound(sp)

            If sp(i).Length > 1 Then

                If sp(i).Substring(1, 1) = "A" Then

                    If sp(i).Substring(sp(i).Length - 1, 1) = "0" Then

                        RichTextBox1.Text = RichTextBox1.Text & sp(i).Replace("0", "Replace with this")

                    End If

                End If

            End If

        Next i
    End Sub
End Class

I have this code but it doesn't seem to work,

i want it so it when it reads through all the file it finds all lines starting with "A" and ending with "0" to be replaced with given string then outputing all of the content from initial text file into richbox1 with the modified values

Dani AI

Generated

Short version: read the file into an array, test each line with StartsWith / EndsWith, remove only the final character and append your replacement, then write the array back to the RichTextBox once. That avoids the “replace every 0” problem and is faster and safer than mutating the RichTextBox text while iterating.

' after you have a valid file path in strFile
Dim lines() As String = System.IO.File.ReadAllLines(strFile)
For i As Integer = 0 To lines.Length - 1
    Dim s As String = lines(i)
    If s.Length > 0 AndAlso s.StartsWith("A") AndAlso s.EndsWith("0") Then
        lines(i) = s.Remove(s.Length - 1) & "Replace with this"
    End If
Next
RichTextBox1.Lines = lines

Notes and edge cases:

  • Use StartsWith("A") instead of checking the second character (that was the indexing slip in the original post).
  • If you want to ignore trailing whitespace when deciding “ends with 0”, check t = s.TrimEnd() and operate on t while preserving the original trailing spaces when you reconstruct the line.
  • Avoid calling RichTextBox1.Text = RichTextBox1.Text.Replace(...) inside the loop: it can replace every matching substring and can be slow for large files. Building a processed array and assigning it once is cleaner.
  • If files are huge, stream and write to a temp file (or use a StringBuilder) to reduce memory churn.

Context from the thread: was on the right track; his initial global Replace caused the multiple-0 issue and his later substring idea matched the correct approach. ’s suggestion to use the last index can be useful for more complex patterns, and ’s ReadLine idea is fine if you guard against empty lines and use EndOfStream properly.

Recommended Answers

All 6 Replies

        Dim ofd As New OpenFileDialog
        Dim strFile As String
        With ofd
            .Title = "Select a List"
            .Filter = "Text Files|*.txt|All Files|*.*"
            .ShowDialog()
            strFile = .FileName
        End With
        RichTextBox1.LoadFile(strFile, RichTextBoxStreamType.PlainText)
        For Each line In RichTextBox1.Lines
            If line.StartsWith("A") Then
                If line.EndsWith("0") Then
                    RichTextBox1.Text = RichTextBox1.Text.Replace(line, line.Replace("0", "Replace with this"))
                End If
            End If
        Next

Tested the code, it replaces every "0" that is in the line, not just the last 0 of the line

Why not look for the LastIndexOf("0")

Click Here

Sorry, Didnt get the question
try this

  Dim ofd As New OpenFileDialog
        Dim strFile As String
        With ofd
            .Title = "Select a List"
            .Filter = "Text Files|*.txt|All Files|*.*"
            .ShowDialog()
            strFile = .FileName
        End With
        RichTextBox1.LoadFile(strFile, RichTextBoxStreamType.PlainText)
        For Each line In RichTextBox1.Lines
            If line.StartsWith("A") Then
                If line.EndsWith("0") Then
                    Dim newline As String
                    newline = line.Substring(0, line.Length - 1) & "Replace with this"
                    RichTextBox1.Text = RichTextBox1.Text.Replace(line, newline)
                End If
            End If
        Next
commented: this works thanks oussama +0

This will probably work:

Dim strFile, strData As String
Dim ln As Integer
Dim Reader As New System.IO.StreamReader(strFile)

    Do
      strData = Reader.ReadLine()
        ln = strData.Length
           If Mid(strData, 1, 1) = "A" And Mid(strData, ln, ln) = "0" Then
             strData = Mid(strData, 1, ln - 1) & " Replace with this"
             RichTextBox1.Text = RichTextBox1.Text & vbNewLine & strData
           End If

    Loop Until strData Is Nothing
commented: untested but thanks i went with oussama's :D +0

Thanks oussama it works perfectly !

commented: you are welcome, mark question solved please +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.