473,654 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between Trim function and Trim member

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
7 4214
The Trim function implemented as a method of the Microsoft.Visua lBasic
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.u ni-koeln.de> wrote in message
news:a5******** *************** ***@posting.goo gle.com...
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
"Stephany Young" <noone@localhos t> 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?F ile=Reflector.z ip>).
'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
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.Substrin g, one would
expect String.Substrin g 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******** ******@TK2MSFTN GP10.phx.gbl...
"Stephany Young" <noone@localhos t> 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?F ile=Reflector.z ip>).
'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
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.Substrin g anyway. What am I missing?

Nov 21 '05 #5
My question, precisely.
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.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.Substrin g anyway. What am I missing?

Nov 21 '05 #6
Stephany,

"Stephany Young" <noone@localhos t> 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
"Stephany Young" <noone@localhos t> 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.Substrin g 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17721
by: DrBob | last post by:
gcc 3.3 MAC OS X. I have a string that has trailing spaces in it that I want removed. So i have a variable: string x("abcd "); x.trim() isn't an implemented method. Is there a method I don't know about? What do I do to extent the string class such that there is a method to
22
12742
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous' or even why there could be a problem. here is the code ////////
11
5711
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public vs. private. I have always just used structs as a minimal class, as that is the way
4
1686
by: JJ_377 | last post by:
Why won't Netscape do the intended in the following? IE is able to render this page as intended. It is is a "self-posting" ASP form with a JavaScript validation function: if the form fails the JavaScript validation function, then the form should not post and an alert should be issued as indicated below. The *first* alert in the Validate function works in NS, but the rest of the code does not and therefore I wonder about the way I am...
2
2562
by: Alex Shirley | last post by:
HI I'm trying to iterate through all the textboxes on a webpage and trim them for spaces. i.e. If a user enters " hello world " we correct it to "hello world" So far I've come up with this:
11
5339
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
22
9727
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use Trim(String), or is it more within etiquette to use If Not String Is Nothing Then String.Trim?
27
6837
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 4+) : String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')") String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')") String.prototype.Trim= new...
5
3516
by: Marjeta | last post by:
I'm trying to very that the user actually entered something in the form, and not just spaces. I guess the problem is in the first line of isBlank() function. I've tried the following: elem.value.trim(); elem.value=elem.value.trim(); elem.value=trim(elem.value); elem.value.replace(/^\s+|\s+$/g,"");and none works. It correctly gives me an error message if I leave the field completely blank.But if there's anything in the field, it does not...
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.