String quickie: Remove trailing space from each RichTextBox line?

P

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
 
R

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
 
S

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 ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top