473,573 Members | 2,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trim(string) vs string.trim

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.Tri m" 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?
Aug 20 '06 #1
22 9724
Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable. Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable. Trim
End If

Whatever you decide, just be consistient.

"Terry Olsen" wrote:
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.Tri m" 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?
Aug 21 '06 #2
Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.

If stringVariable IsNot Nothing Then
stringVariable = stringVariable. Trim
End If

/claes

"Jared" <Ja***@discussi ons.microsoft.c omwrote in message
news:24******** *************** ***********@mic rosoft.com...
Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable. Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable. Trim
End If

Whatever you decide, just be consistient.

"Terry Olsen" wrote:
>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.Tri m" 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?

Aug 21 '06 #3
Claes,
Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.
Maybe for native English people, I find it terrible to read.

Cor

"Claes Bergefall" <lo*****@nospam .nospamschreef in bericht
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.

If stringVariable IsNot Nothing Then
stringVariable = stringVariable. Trim
End If

/claes

"Jared" <Ja***@discussi ons.microsoft.c omwrote in message
news:24******** *************** ***********@mic rosoft.com...
>Terry,
You should always check your parameters/variables for null references
whenever there is doubt.

You can use the negating logic if you prefer, its just not as straight
forward (it is here, I just avoid negating logic whenever possible)
If Not Is Nothing Then
stringVariable = stringVariable. Trim
End If

If stringVariable is nothing then
stringVariable = string.empty
Else
stringVariable = stringVariable. Trim
End If

Whatever you decide, just be consistient.

"Terry Olsen" wrote:
>>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.Tri m" 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?


Aug 21 '06 #4

Terry Olsen wrote:
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.Tri m" 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?
The advantage of using Trim instead of String.Trim is exactly that Trim
will recognize when a String is Nothing and return "" as a result. If
this is the logic of your application, then instead of doing:

If SomeStr Is Nothing then
Value = ""
'I personally preffer Value = String.Empty
Else
Value = String.Trim
End If

you could spare the effort and just use Value = Trim(SomeStr)

On the other hand, if you must know when a passed string is invalid
(Nothing) then probably checking for Nothing before calling String.Trim
is the way to go.

Regards,

Branco.

Aug 21 '06 #5
Yes, after all the input, I have decided to leave it as String=Trim(Str ing)

"Branco Medeiros" <br************ *@gmail.comwrot e in message
news:11******** **************@ m79g2000cwm.goo glegroups.com.. .
>
Terry Olsen wrote:
>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.Tri m" 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?

The advantage of using Trim instead of String.Trim is exactly that Trim
will recognize when a String is Nothing and return "" as a result. If
this is the logic of your application, then instead of doing:

If SomeStr Is Nothing then
Value = ""
'I personally preffer Value = String.Empty
Else
Value = String.Trim
End If

you could spare the effort and just use Value = Trim(SomeStr)

On the other hand, if you must know when a passed string is invalid
(Nothing) then probably checking for Nothing before calling String.Trim
is the way to go.

Regards,

Branco.

Aug 22 '06 #6
Cor Ligthert [MVP] wrote:
Claes,
>Don't forget the new IsNot operator that was introduced in 2.0. It's a bit
easier to read imo.

Maybe for native English people, I find it terrible to read.
Cor,

I *am* English and I think the "Eye-Snot" operator is appalling.

I've been using something like

Function IsSomething( ByVal oThing As Object ) As Boolean
Return Not (oThing Is Nothing )
End Function

(in various guises) for more years than I like to think about and have
no intention of changing.

OK, there's an overhead in calling a function, but any half-decent
compiler would optimise that out.

Regards,
Phill W.
Aug 22 '06 #7
Phill,

In your sample it is a boolean.

My is told that English people seem to use IsNot Nothing when it is
Something.

I prefer "something" because I hate those possible constructions as it is in
VB.Net as
If Not (Not ObjectAddress IsNot Nothing) what is a valid instruction to
return a boolean if that address is something.

But as me is told is this for English people easy readable.

Not all spoken language are always algabraic correct do you know.

Cor

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kschreef in bericht
news:ec******** **@south.jnrs.j a.net...
Cor Ligthert [MVP] wrote:
>Claes,
>>Don't forget the new IsNot operator that was introduced in 2.0. It's a
bit easier to read imo.

Maybe for native English people, I find it terrible to read.

Cor,

I *am* English and I think the "Eye-Snot" operator is appalling.

I've been using something like

Function IsSomething( ByVal oThing As Object ) As Boolean
Return Not (oThing Is Nothing )
End Function

(in various guises) for more years than I like to think about and have no
intention of changing.

OK, there's an overhead in calling a function, but any half-decent
compiler would optimise that out.

Regards,
Phill W.

Aug 22 '06 #8
Ok, not being formally trained in the art of coding, what exactly is
apalling about IsNot? It seems like a perfectly valid and logical option to
me. Your method is like making 3 left turns to get to the store when a
single right turn would have gotten you there, simply because you don't like
right turns.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ec******** **@south.jnrs.j a.net...
Cor Ligthert [MVP] wrote:
>Claes,
>>Don't forget the new IsNot operator that was introduced in 2.0. It's a
bit easier to read imo.

Maybe for native English people, I find it terrible to read.

Cor,

I *am* English and I think the "Eye-Snot" operator is appalling.

I've been using something like

Function IsSomething( ByVal oThing As Object ) As Boolean
Return Not (oThing Is Nothing )
End Function

(in various guises) for more years than I like to think about and have no
intention of changing.

OK, there's an overhead in calling a function, but any half-decent
compiler would optimise that out.

Regards,
Phill W.

Aug 22 '06 #9
"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:uM******** ******@TK2MSFTN GP05.phx.gbl...
Claes,
>Don't forget the new IsNot operator that was introduced in 2.0. It's a
bit easier to read imo.

Maybe for native English people, I find it terrible to read.

Cor
Well, I'm Swedish and I find it a lot better than using 'If Not ... Is
Nothing...'. That just looks weird. Until they add support for 'If ... Is
Something' I'm sticking with the IsNot operator

/claes
Aug 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
12730
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 ////////
17
4647
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if(...
11
5336
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
7
4212
by: Sascha Herpers | last post by:
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.
5
17371
by: Pete | last post by:
How do you get rid of leading white spaces in a string in vb.net? From http://www.developmentnow.com/g/38_2005_9_1_0_0/dotnet-languages-vb.ht Posted via DevelopmentNow.com Group http://www.developmentnow.com
10
2712
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to keep the integrity of the database. We are parsing roughl 81 files and range in size 1 kb to 65 MB files (Average of 400,000 lines in the larger...
3
3491
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i read in msdn : and notice that trim only Removes all occurrences of white space characters from the beginning and end of this instance. So what for the...
31
2881
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char *trim(char *s) { char *begin,*end; begin = s;
1
6048
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while during the runtime, the user can click on a button and change the connection string without exiting the application. I would really appreciate...
5
1882
by: =?Utf-8?B?c2VlbWE=?= | last post by:
Hi, I have a string line = " DOL : 12/04/09 " I want to remove the white spaces and to get the string like line = "DOL : 12/04/09" ; i have th e following coding but it is not going to if statement. line.Trim(); if (line.StartsWith("DOL"))
0
8196
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...
1
7768
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...
0
8058
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...
0
6400
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...
1
5579
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...
0
5288
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...
0
3729
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...
1
2191
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
0
1035
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...

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.