1.11M Members

reads all lines in text file and be able to edit many lines etc

 
0
 
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

 
1
 
        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
 
0
 

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

 
0
 

Why not look for the LastIndexOf("0")

Click Here

 
2
 

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
 
1
 

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
 
1
 

Thanks oussama it works perfectly !

Question Answered as of 6 Months Ago by oussama_1, Minimalist and kRod
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article