Trim returns and line feeds

View member feedback to this tip.

I had to create this function because we have address fields set to OS Style and they can display multiple lines. Users entered addresses with carriage returns to change to a new line and the fields often ended up with extra carriage returns. We have code that traps changes in the address fields. When comparing the previous and actual addresses, if one contained extra returns and nothing else was changed, the values were considered unequal with the trim function. I created this TrimReturns function so addresses in which only trailing returns are the difference would actually be compared as equals.

The following is a LotusScript function that trims the returns and line feeds from a string.

Function TrimReturns(inputString 
As String) As String
 'This function removes the trailing 
carriage returns and line feeds from a string 
and returns the input string without these 
 'Code by Benoit Dubuc
 Dim position As Integer
 Dim lastChar As String
 position = Len(inputString)
 lastChar = Right(inputString, 1)
 While Asc(lastChar) = 10 
Or Asc(lastChar) = 13
  position = position -1
  lastchar = Right ( Left(inputstring,
 position), 1)
 TrimReturns = Left(inputString, 
End Function


Doesn't the LotusScript FullTrim function accomplish the same thing?

-- Joseph S.


No, the fullTrim function leaves the trailing carriage returns. It only trims spaces. I think FullTrim was mainly built to trim arrays from empty entries.

-- Benoit Dubuc


You may already know this but in R6, you can do this natively with the Replace function:

Dim strText As String

strText$ = "My name: " & Chr(13) 
& "Dallas G." & Chr(13)
strText$ = strText$ & Chr(13) & 
"My email: " & Chr(13) &
companyname_dot_com" & Chr(13)
strText$ = strText$ & Chr(13) & 
"My number: " & Chr(13) & "(806)555-6251"

Msgbox strText$, , "DEBUG . . ."

strText$ = Replace(strText$, Chr(13), "-")
Msgbox strText$, , "DEBUG . . ."
-- Dallas G.


This is a great tip. I was needing to remove only trailing carriage returns and the Replace function removes all of the Carriage returns. Benoit's function accomplished exactly what I needed with very minimal code.

-- Mark H.

Do you have comments of your own? Let us know.

This was first published in September 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.