Difference between Trim function and Trim member

  • Thread starter Thread starter Sascha Herpers
  • Start date Start date
S

Sascha Herpers

Hi,

what is the difference between the trim function and the trim
String-member?
As far as I see it, both return the trimmed string and leave the
original string unaltered.
Is any of the two faster? Is there a general rule/opinion to prefere
members over functions?

Thanks for any hint.

Sascha
 
The Trim function implemented as a method of the Microsoft.VisualBasic
namespace and acts identically to the VB6 Trim function. I.E., It returns a
string containing a copy of a specified string with no leading or trailing
spaces.

The Trim method is implemented as a method of the System.String class and it
returns a new string equivalent to the specified string after white space
characters are removed from the beginning and end. In addition, this method
has an overload where one can specify which character(s) should be removed.

Calling the Trim function is the same as VB6:
_s = Trim(_s)
whereas the Trim method operates on a string instance:
_s = _s.Trim()

Because white space comprises characters in addition to the space character,
if you want to remove only spaces then you must call the the Trim function
or the overloaded Trim method with a parameter of " "c.

I have run tests that show that using the Trim method gives a marginal
performance saving, and I stress the word marginal. Any other evidence I
have heard or read on the performance of the Trim function over the Trim
method or vice-versa is purely anecdotal.
 
Stephany Young said:
I have run tests that show that using the Trim method gives a marginal
performance saving, and I stress the word marginal. Any other evidence I
have heard or read on the performance of the Trim function over the Trim
method or vice-versa is purely anecdotal.

I took a look at the implementation of 'String.Trim' and 'Strings.Trim'
using Reflector
(<URL:http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector.zip>).
'Strings.Trim' performs some checks first and then calls 'String.Trim'. My
conclusion is that it's up to personal preference whether to use
'String.Trim' or 'Strings.Trim'.
 
Agreed.

That 'Reflector' shows some interesting things.

Looking at Strings.Trim, it is understandable that String.Trim is marginally
faster.

By the same token, looking at Strings.Mid v String.Substring, one would
expect String.Substring to be marginally faster, however, observation shows
that Strings.Mid is actually the faster method.

Curiouser and curioser ...
 
I'm confused. While I don't doubt you because I have not run any
tests, but why would Strings.Mid be any faster? It just calls
String.Substring anyway. What am I missing?
 
Stephany,

Stephany Young said:
Looking at Strings.Trim, it is understandable that String.Trim is
marginally faster.

I didn't yet take a look at the implementation of 'String.Trim', but I am
curious why the VB.NET team decided to check the value passed to the
function before calling the string's 'Trim' method. It depends on the
implementation of the 'TrimHelper' method if this makes sense or not, and if
it increases the performance or not.
 
Stephany Young said:
My question, precisely.

That's hard to decide as long as we don't know anything about the internal
implementation of 'Substring'.
 
True, but in the implementation for Strings.Mid, there doesn't seem to
be anything that would make its call to String.Substring any faster
than a direct call to String.Substring. I don't see how the internal
implementation of substring is relevant.

When I look at the code for String.Substring, I see this:

Public Function Substring(ByVal startIndex As Integer, ByVal length As
Integer) As String
Dim num1 As Integer = Me.Length
If (startIndex < 0) Then
Throw New ArgumentOutOfRangeException(...)
End If
If (length < 0) Then
Throw New ArgumentOutOfRangeException(...)
End If
If (startIndex > (num1 - length)) Then
Throw New ArgumentOutOfRangeException(...)
End If
Dim text1 As String = String.FastAllocateString(length)
String.FillSubstring(text1, 0, Me, startIndex, length)
Return text1
End Function

And when I look at the code for Strings.Mid I see this:

Public Shared Function Mid(ByVal str As String, ByVal Start As Integer,
ByVal Length As Integer) As String
If (Start <= 0) Then
Throw New ArgumentException(...)
End If
If (Length < 0) Then
Throw New ArgumentException(...)
End If
If ((Length = 0) OrElse (str Is Nothing)) Then
Return ""
End If
Dim num1 As Integer = str.Length
If (Start > num1) Then
Return ""
End If
If ((Start + Length) > num1) Then
Return str.Substring((Start - 1))
End If
Return str.Substring((Start - 1), Length)
End Function

Except for the few additional checks that Mid performs, it is just a
call to Substring. what would make Mid run faster? To me, it should
run slower because of the extra checks. I am still confused.
 

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

Similar Threads


Back
Top