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

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.