none
If line starts with a space remove that line? RRS feed

  • Question

  • I have a RichTextBox and in there I have multiple lines of text. How can I search through each line and if that line starts with a space then it will remove that entire line? I've searched for help but couldn't find any. It sounds like basic code using the Loop function but since I am somewhat a newbie to the VB.NET language I'm not sure what to write. Thanks! :^)

    Monday, January 10, 2011 12:25 AM

Answers

  • Without testing, I would think that your code needs to be:

         If Not (line.Length > 0 AndAlso line(0) = " ") Then
           cleanedtext.AppendLine(Line)
         End If

    • Marked as answer by -Alex Monday, January 10, 2011 1:03 AM
    Monday, January 10, 2011 12:55 AM
  • try this in an app with a button and richtextbox:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim sb As New StringBuilder
    
        For Each l As String In RichTextBox1.Lines
          If Not l.StartsWith(" ") AndAlso Not l.Length = 0 Then
            sb.Append(l & vbCrLf)
          End If
        Next
    
        RichTextBox1.Text = sb.ToString
    
      End Sub
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Text = "Line 1" & vbCrLf & vbCrLf & " Line 2" & vbCrLf & "Line3" & vbCrLf & " Line 4"
      End Sub
    • Marked as answer by -Alex Monday, January 10, 2011 1:03 AM
    Monday, January 10, 2011 12:59 AM

All replies

  • Oops I forgot here is the code I've tried:

     

     

    Dim cleanedtext As New System.Text.StringBuilder
     Using cleaner As New System.IO.StringReader(RichTextBox1.Text)
     Dim line As String = cleaner.ReadLine
     While line IsNot Nothing
      If line.Length > 0 AndAlso line(0) = " " Then
      line.Remove(1)
      End If
      cleanedtext.AppendLine()
    
      line = cleaner.ReadLine
     End While
     End Using
     RichTextBox1.Text = cleanedtext.ToString
    

     

    But it deletes ALL of my lines, not just the ones that start with a space. Something I'm doing wrong?

    Monday, January 10, 2011 12:46 AM
  • Without testing, I would think that your code needs to be:

         If Not (line.Length > 0 AndAlso line(0) = " ") Then
           cleanedtext.AppendLine(Line)
         End If

    • Marked as answer by -Alex Monday, January 10, 2011 1:03 AM
    Monday, January 10, 2011 12:55 AM
  • try this in an app with a button and richtextbox:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim sb As New StringBuilder
    
        For Each l As String In RichTextBox1.Lines
          If Not l.StartsWith(" ") AndAlso Not l.Length = 0 Then
            sb.Append(l & vbCrLf)
          End If
        Next
    
        RichTextBox1.Text = sb.ToString
    
      End Sub
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Text = "Line 1" & vbCrLf & vbCrLf & " Line 2" & vbCrLf & "Line3" & vbCrLf & " Line 4"
      End Sub
    • Marked as answer by -Alex Monday, January 10, 2011 1:03 AM
    Monday, January 10, 2011 12:59 AM
  • Thanks to both, they both work wonderfully. :)
    Monday, January 10, 2011 1:04 AM