473,839 Members | 1,371 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 4228
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
17752
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
12779
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
5725
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
1698
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
2577
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
5365
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
9763
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
6886
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
3529
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
10908
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
10586
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
10648
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
9426
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...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
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...
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.