By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

Difference between Trim function and Trim member

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
"Sascha Herpers" <he*****@wiso.uni-koeln.de> wrote in message
news:a5**************************@posting.google.c om...
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

Nov 21 '05 #2

P: n/a
"Stephany Young" <noone@localhost> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
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 ...
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ou**************@TK2MSFTNGP10.phx.gbl...
"Stephany Young" <noone@localhost> schrieb:
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

P: n/a
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?

Nov 21 '05 #5

P: n/a
My question, precisely.
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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?

Nov 21 '05 #6

P: n/a
Stephany,

"Stephany Young" <noone@localhost> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

P: n/a
"Stephany Young" <noone@localhost> schrieb:
My question, precisely.
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?


That's hard to decide as long as we don't know anything about the internal
implementation of 'Substring'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.