 |

03-22-2012, 10:28 AM
|
|
Newcomer
|
|
Join Date: Mar 2012
Posts: 1
|
|
Remove trailing line breaks
|
Hey.. 10 years later.. Referencing this thread
This function was close but didn't work for me.
I made a slight modification to it, as shown below, and now it works well to remove one or more trailing line breaks.
Note that I have the "option compare text" setting in the module I tested this with.
The string I tested it with was as follows:
Code:
Dim ttt As String
ttt = "one" & vbCrLf & "two" & vbCrLf & "three" & vbCrLf & "." & vbCrLf
Code:
Private Function Remove_Line_Break(ByVal MyLine As String) As String
Dim X As Integer
Dim strX As String
For X = Len(MyLine) To 1 Step -2
strX = Mid$(MyLine, X + 1, 2)
If strX >= "a" Then GoTo Finished
If strX = vbCrLf Then
MyLine = Left$(MyLine, Len(MyLine) - 2)
End If
Next X
Finished:
Remove_Line_Break = MyLine
End Function
A belated thank you very much, for pointing me in the right direction with this 
|
Last edited by passel; 03-29-2012 at 02:49 PM.
|

03-22-2012, 11:38 AM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,922
|
|
Welcome to the forum mrfoot. Don't forget to read the forum Posting Guidelines.
Same thing but simplified and for any character or number of characters.
Code:
Option Explicit
Private Sub CommandButton1_Click()
Dim Test As String
' This could be text ending in multiple vbCrLf characters.
Test = "This is a test*!*!*!*!*!*!"
MsgBox RTrimChars(Test, "*!") & "---"
End Sub
Public Function RTrimChars(ByVal Myline As String, ByVal TrimChars As String) As String
Dim CharsSize As Integer
CharsSize = Len(TrimChars)
Do Until Right$(Myline, CharsSize) <> TrimChars
Myline = Left$(Myline, Len(Myline) - CharsSize)
Loop
RTrimChars = Myline
End Function
Edit:
Doh! Didn't notice this was a grave dig.
Please do not post to threads where the last response was older than a couple of weeks as per forum guidelines. Start a new thread instead.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
Last edited by Gruff; 03-22-2012 at 12:01 PM.
|

03-22-2012, 11:41 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,419
|
|
Gravedigging is generally frowned upon, but I'm poking my head in because none of the answers are correct.
In general, Mid$(), Len(), and other structured-programming-based VB6 methods should make a .NET programmer go "Huh?". It's been nearly 10 years; I'm not sure why people are reaching for them first. .NET programmers aren't familiar with them unless they had prior VB6 experience. This is a more .NET solution:
Code:
myLine = myLine.TrimEnd()
That'll trim all whitespace from the end of the line. Maybe you /only/ want CRLF. Easy enough, though you must note vbCrLf is in fact two characters:
Code:
myLine = myLine.TrimEnd(vbCrLf.ToCharArray())
Don't reinvent the wheel!
|
|

03-22-2012, 11:47 AM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,922
|
|
|
I agree with you completely Atma. In this case though I do not think the op is using VB.NET.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

03-29-2012, 01:45 AM
|
 |
Contributor
|
|
Join Date: Jun 2003
Location: NW UK
Posts: 405
|
|
Am I missing something here? The original question from a decade ago was:
Quote:
|
What is the best way to trim trailing line breaks (vbCrLf) from a string? Similar to RTrim to remove spaces. Do I have to loop and remove one by one?
|
Surely he/she must have been referring to text from a RichTextBox (or similar) and basically just needed to access each line separately? In which case:
Code:
Dim strLine() as String
strLine = Split(RTB.Text, vbCrLf)
Then line 1 is in strLine(1) and so forth. Simples.
.
|
|

03-29-2012, 10:01 AM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,922
|
|
|
Actually no Bob. It sounds like your suggesting the same thing as
myString = replace(mystring,vbCrLf,"")
The op was asking about removing trailing vbCrLf characters only.
P.S. the first element of the result of the split() function is zero not one.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

03-29-2012, 10:34 AM
|
 |
Contributor
|
|
Join Date: Jun 2003
Location: NW UK
Posts: 405
|
|
Quote:
Originally Posted by Gruff
Actually no Bob. It sounds like your suggesting the same thing as
myString = replace(mystring,vbCrLf,"")
The op was asking about removing trailing vbCrLf characters only.
|
Gruff. Yes, but by definition a vbCrLf can ONLY be at the end. That is the definition of the end? Or am I up another gumtree?
I reckon we are both right and the question is wrong ... LOL
Bob
PS
Quote:
Originally Posted by Gruff
PS the first element of the result of the split() function is zero not one.
|
I claim that Line Zero is strArray(0) and Line 1 is strArray(1)
If you can have the Square Root of -1 I think you can have a line zero
.
|
|

03-29-2012, 12:38 PM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,922
|
|
Gumtrees abound. 
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
|

03-29-2012, 03:30 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
|
|
Bob, I think the desire was to remove blank lines from the end of a file (the trailing line breaks).
You do a single read of the whole file into a string and remove the multiple line breaks at the end.
Gruff, its best to avoid string manipulations if you can, as you'll be creating new strings each time, most likely. Especially true in .Net, which this is not, but still true.
So, I changed your loop to scan the string, but only modify the string, once the "end" is found. (added a couple of "idiot proofing" tests as well)
Code:
Public Function RTrimChars(ByVal Myline As String, ByVal TrimChars As String) As String
Dim CharsSize As Integer, p As Long
CharsSize = Len(TrimChars)
p = 1 + Len(Myline) - CharsSize
If p > 0 Then 'If the string is shorter than our pattern don't bother causing an exception
Do Until Mid$(Myline, p, CharsSize) <> TrimChars 'look from the end of the string until we don't match
p = p - CharsSize
If p < 1 Then Exit Do 'Someones playing around, its filled with the pattern, we'll return an empty string
Loop
End If
RTrimChars = Left$(Myline, p + CharsSize - 1)
End Function
I guess AtmaWeapon must have step in something slippery and slid into the legacy VB forum. Would have though the smell would have alerted him.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

03-30-2012, 01:10 AM
|
 |
Contributor
|
|
Join Date: Jun 2003
Location: NW UK
Posts: 405
|
|
Quote:
Originally Posted by passel
Bob, I think the desire was to remove blank lines from the end of a file
|
Well, the original question was:
Quote:
Hi,
What is the best way to trim trailing line breaks (vbCrLf) from a string? Similar to RTrim to remove spaces. Do I have to loop and remove one by one?
Thanks,
Glenn
|
I see no reference to blank lines or files! This could become a religion ... gradually accumulating myths like a snow ball rolling down a hill and the protagonists selectively quoting each other to mislead the spectators LOL
Bob
.
|
|

03-30-2012, 09:45 AM
|
 |
Bald Mountain Survivor
Super Moderator * Expert *
|
|
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,922
|
|
Thanks for the update Passel you are right of course.
Finding the start of the trailing blank lines and making only one
Left$() clip is much more efficient than multiple clipping in a loop. What was I thinking?
In any case my original point was simply that making a general tool that can Trim any trailing character or group of characters makes more sense than a one trick pony. Similar to the VB.NET TrimEnd() tool overload.
Bob,
A blank line is one where the vbCrLf has nothing after it or another vbCrLf.
Example:
s = "This is a test" & vbcrlf & This should be the last line" & vbcrlf & vbcrlf & vbcrlf
There are three trailing blank lines in the above string. The one followin "this is a test" is not trailing so is not removed.
---
With respect,
If you actually showed some code you tested that explained what you were offering it would have been clearer than a general statement about using the split function. Although re-reading your posts in this thread I think you are tweaking our noses in jest.
|
__________________
Burn the land and boil the sea
You can't take the sky from me
~T
Last edited by Gruff; 03-30-2012 at 05:18 PM.
|

03-30-2012, 12:05 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,722
|
|
Quote:
Originally Posted by bobcory
Well, the original question was:
Quote:
|
What is the best way to trim trailing line breaks (vbCrLf) from a string? Similar to RTrim to remove spaces. Do I have to loop and remove one by one?
|
I see no reference to blank lines or files! This could become a religion ... gradually accumulating myths like a snow ball rolling down a hill and the protagonists selectively quoting each other to mislead the spectators LOL
Bob
.
|
A bit of sophistry, Bob, given your statement:
Quote:
Originally Posted by bobcory
Surely he/she must have been referring to text from a RichTextBox (or similar) and basically just needed to access each line separately?
|
There was no reference to a RichTextBox or needing to access separate lines stated either.
Quote:
Originally Posted by bobcory
Gruff. Yes, but by definition a vbCrLf can ONLY be at the end. That is the definition of the end? Or am I up another gumtree?
|
I'm not sure how the gumtree applies but assuming the assertion that one side of the Or condition must be True, then you must be up a gumtree.
A vbCrLF is not only at the end of a string (remember the original question, how to move trailing line breaks from a String). A linebreak can occur thoughout a string indicating the string should be "broken" at this point and printing resumed on a new line of your output (a monitor, or printer, etc).
A linebreak is a formating convention so can occur at any point in a string.
In English, the "s" at the end of a noun often means we're talking about multiple instances of the noun, as in "trailing line breaks".
Also, in English, the article "a" would indicate a singular instance of some thing, i.e. a car, a plane, a string.
The word trailing is used before a "thing" to indicate multiple instances of that particular thing at the end of a list (or array, or string) of things, such as trailing zeros, trailing spaces, trailing line breaks.
The RTrim functions was mentioned to illustrate what the person was looking for to help avoid confusion.
The RTrim function removes multiple whitespace characters from the "right" end of a string.
It doesn't remove the spaces in the string.
In doesn't separate the string into an array of strings, throwing away the "separaters" (the spaces).
It removes the trailing whitespace, leaving you a single string without the extra space at the end.
So, he was looking for a function that was similiar to RTrim in its action, but removed trailing line breaks from the end of the string.
So, that requirement meant, to the original person who replied, and to the person who modified it 10 years later:
A function that takes a string, removes the extra line breaks at the end of the string, returning the trimmed string.
Looking at all the code posted, every one of them (including AtmaWeapon's) met that requirement with one exception, yours.
The original poster was also clear on the type of line break (vbCrLf), as he was probably aware that some text files (Unix, Linux, etc) use (vbLf) for line breaks, and some text files (Mac, DEC, etc) use (vbCr) for line breaks.
You are right, he didn't mention a text file, I was just illustrating that that is one common case where you may create a string from the file and have multiple line breaks at the end of the string.
Your textbox is another case, as the text property is a single string, and if the user hits Enter multiple times at the end of the text, will have a single string with multiple line breaks at the end of the string.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

03-30-2012, 04:25 PM
|
 |
Underclocked lifestyle
Forum Leader * Guru *
|
|
Join Date: Feb 2005
Location: Michigan, USA
Posts: 4,193
|
|
So far this thread reads like an early April Fool's Day attempt. I won't quote the many "funnies" because the thread is too full of long posts already.
Anyway, here's my contribution:
Code:
Private Function RTrimStrings(ByVal Text As String, ByVal TrimString As String) As String
Dim Max
Dim I As Long
Dim J As Long
Max = Len(TrimString) - 1
If Max < 0 Then Err.Raise 5, , "TrimString must be at least 1 character long"
I = Len(Text)
Do While I > 0
J = InStrRev(Text, TrimString, I)
If J = 0 Or I - J > Max Then Exit Do
I = J - 1
Loop
RTrimStrings = Left$(Text, I)
End Function
|
Last edited by dilettante; 03-30-2012 at 04:32 PM.
Reason: Added the error check
|

03-31-2012, 06:29 AM
|
 |
Contributor
|
|
Join Date: Jun 2003
Location: NW UK
Posts: 405
|
|
Quote:
Originally Posted by bobcory
This could become a religion ... gradually accumulating myths like a snow ball rolling down a hill and the protagonists selectively quoting each other to mislead the spectators LOL
|
Well, I think I got that right!
Actually, I will confess that I had not thought of a String Array where each element of the array has several vbCrLf entries so I plead guilty to that
Quote:
|
If you actually showed some code you tested that explained what you were offering it would have been clearer than a general statement about using the split function
|
Well actually I did post some code:
Code:
Dim strLine() as String
strLine = Split(RTB.Text, vbCrLf)
Quote:
|
Although re-reading your posts in this thread I think you are tweaking our noses in jest.
|
Again, slightly guilty.
As a general point I have to say I find this forum incredibly useful and have not the slightest desire to upset anybody so I apologise if I have offended anybody. Over the last nine years dozens of people have spent a lot of time helping me and I really do appreciate that.
Hopefully, people in the future who want to do things with strings containing vbCrLf will find these posts useful
Kind Regards to All
Bob
.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|