Home All Groups Group Topic Archive Search About

String quickie: Remove trailing space from each RichTextBox line?

Author
7 Sep 2006 11:43 PM
Paul
Hi,

My RichTextBox has multiple lines of text.  Most of the lines unfortunately
end with a space.

Is it possible to replace the space and NewLine/Line Feed with just the
NewLine/LineFeed?
So in essence just removing the trailing space from each line?


Thanks,
Paul

Author
8 Sep 2006 4:10 AM
rowe_newsgroups
You might try the below code (not tested)

Dim str As String = ""
Dim i As Integer

' Loop through the characters in the text box
For i = 1 To RichTextBox1.TextLength
     ' Check the ascii values of the current character
     ' Note, an ascii value of 10 indicates enter
     If Asc(Mid(RichTextBox1.Text, i, 1)) = 10 Then
         ' trim the string to remove trailing spaces
         str = Trim(str) & vbCrLf
     Else
         str += Mid(RichTextBox1.Text, i, 1)
     End If
Next i

RichTextBox1.Text = str

Thanks,

Seth

Paul wrote:
Show quoteHide quote
> Hi,
>
> My RichTextBox has multiple lines of text.  Most of the lines unfortunately
> end with a space.
>
> Is it possible to replace the space and NewLine/Line Feed with just the
> NewLine/LineFeed?
> So in essence just removing the trailing space from each line?
>
>
> Thanks,
> Paul
Author
8 Sep 2006 1:14 PM
S Kachru
Dim str As String 'Your string
            Dim arr() As String = str.Split(New Char() {ControlChars.CrLf})
            For i As Integer = 0 To arr.Length - 1
                arr(i) = arr(i).TrimEnd(" ")
            Next
            str = String.Join(ControlChars.CrLf, arr)

There might be a better way than this ...



Show quoteHide quote
> Hi,
>
> My RichTextBox has multiple lines of text.  Most of the lines
> unfortunately end with a space.
>
> Is it possible to replace the space and NewLine/Line Feed with just
> the
> NewLine/LineFeed?
> So in essence just removing the trailing space from each line?
> Thanks,
> Paul
Author
8 Sep 2006 2:06 PM
S Kachru
Or try this...

            str = Regex.Replace(str, "(\s)+\r\n", ControlChars.CrLf)


Show quoteHide quote
> Dim str As String 'Your string
> Dim arr() As String = str.Split(New Char()
> {ControlChars.CrLf})
> For i As Integer = 0 To arr.Length - 1
> arr(i) = arr(i).TrimEnd(" ")
> Next
> str = String.Join(ControlChars.CrLf, arr)
> There might be a better way than this ...
>
>> Hi,
>>
>> My RichTextBox has multiple lines of text.  Most of the lines
>> unfortunately end with a space.
>>
>> Is it possible to replace the space and NewLine/Line Feed with just
>> the
>> NewLine/LineFeed?
>> So in essence just removing the trailing space from each line?
>> Thanks,
>> Paul