473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

checking for empty string

which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0
Nov 16 '05 #1
11 4606
Checking the length is better than checking if the string is empty or not :)

Bye.

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9
"Dan Bass" wrote:
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0

Nov 16 '05 #2
I usually go with:
MyString == null || MyString.Length == 0
Poking around a bit with Lutz Roeder's .Net Reflector, it seems that this
version is the equivalent of MyString == null || MyString.m_stri ngLength == 0
while the other version if the equivalent of
MyString == null || CultureInfo.Cur rentCulture.Com pareInfo.Compar e(MyString, new String (""),
CompareOptions. None) == 0

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. .. which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0

Nov 16 '05 #3
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0


Well, the latter is slightly more efficient, so you should use that if
this is a bottleneck in your application, but really it's a case of
whichever you find most readable, in most cases. I personally prefer
the first, but it's really a matter of taste.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
I use

MyString == null || MyString == string.Empty

as I think its the clearest with hard coded values

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0

Nov 16 '05 #5
should say "without hard coded values"

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<#I************ **@TK2MSFTNGP12 .phx.gbl>

I use

MyString == null || MyString == string.Empty

as I think its the clearest with hard coded values
Nov 16 '05 #6
It's a trade off between a slight advantage in clarity vs a slight advantage
in performance (actually MyString == "" has something more than a slight
performance advantage in theory, but in practice it isn't that often that it
amounts to so much of a difference that a user would actually notice).

I personally use MyString.Length == 0 out of habit, as I think it's pretty
self-explanatory.

As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it. It would also have the advantage of being utterly clear, except
that in my experience many developers seem fuzzy on exactly what Empty
really means. I mean, *I'm* clear on what it means yet when I first
encountered String.Empty I felt it necessary to check the docs to make sure
it meant what I thought it did. And most languages have an unambiguous way
to express an empty string as a constant similar to "", so I prefer "" as
clearer than String.Empty.

--Bob

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0

Nov 16 '05 #7
> As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it
Not exactly ;)

The post of Brad Adams
(http://blogs.msdn.com/brada/archive/.../22/49997.aspx) confirm that
using "" create an object while String.Empty create no onject.

So if you are really looking for ultimately in memory efficiency, use
String.empty

Bye.

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9

"Bob Grommes" wrote:
It's a trade off between a slight advantage in clarity vs a slight advantage
in performance (actually MyString == "" has something more than a slight
performance advantage in theory, but in practice it isn't that often that it
amounts to so much of a difference that a user would actually notice).

I personally use MyString.Length == 0 out of habit, as I think it's pretty
self-explanatory.

As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it. It would also have the advantage of being utterly clear, except
that in my experience many developers seem fuzzy on exactly what Empty
really means. I mean, *I'm* clear on what it means yet when I first
encountered String.Empty I felt it necessary to check the docs to make sure
it meant what I thought it did. And most languages have an unambiguous way
to express an empty string as a constant similar to "", so I prefer "" as
clearer than String.Empty.

--Bob

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
which one do you use and why?

MyString == null || MyString == ""

vs

MyString == null || MyString.Length == 0


Nov 16 '05 #8
I take his statement with a grain of salt. String.Empty is a string
instance that presumably already exists as he states (it's a static
instance), but "" not just "probably" but *wil*" come out of the string
intern pool, as do all string constants in programs. As even that blogger
admits, the difference would be insignificant in any case. And I've seen
more than one other confident statement that String.Empty and "" are exactly
identical. Personally I don't have the time to go through the Rotor source
to figure it out.

The truth is that these are the little rationalization s we create to
validate our favorite way of doing things. Value == "", Value ==
String.Empty, and String.Length == 0 are in fact all perfectly fine and in
almost all real-world scenarios even String.Length == 0 will not produce a
*significant* (user-noticeable) difference in performance.

Only we geeks can thoroughly debate a topic this unimportant to death ;-)

--Bob

"LEBRUN Thomas" <lebrun_thomas_ at_hotmail.com> wrote in message
news:02******** *************** ***********@mic rosoft.com...
As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it


Not exactly ;)

The post of Brad Adams
(http://blogs.msdn.com/brada/archive/.../22/49997.aspx) confirm that
using "" create an object while String.Empty create no onject.

So if you are really looking for ultimately in memory efficiency, use
String.empty

Bye.

-------------------
LEBRUN Thomas
http://morpheus.developpez.com
http://blog.developpez.com/index.php?blog=9

"Bob Grommes" wrote:
It's a trade off between a slight advantage in clarity vs a slight
advantage
in performance (actually MyString == "" has something more than a slight
performance advantage in theory, but in practice it isn't that often that
it
amounts to so much of a difference that a user would actually notice).

I personally use MyString.Length == 0 out of habit, as I think it's
pretty
self-explanatory.

As an aside, String.Empty is completely equivalent to MyString == "",
performance-wise -- String.Empty is simply a language-independent way to
express it. It would also have the advantage of being utterly clear,
except
that in my experience many developers seem fuzzy on exactly what Empty
really means. I mean, *I'm* clear on what it means yet when I first
encountered String.Empty I felt it necessary to check the docs to make
sure
it meant what I thought it did. And most languages have an unambiguous
way
to express an empty string as a constant similar to "", so I prefer "" as
clearer than String.Empty.

--Bob

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
> which one do you use and why?
>
> MyString == null || MyString == ""
>
> vs
>
> MyString == null || MyString.Length == 0
>


Nov 16 '05 #9
LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
So if you are really looking for ultimately in memory efficiency, use
String.empty


FxCop tells me that testing String.Length against 0 is more efficient.
Nov 16 '05 #10

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

Similar topics

3
3243
by: Antoni | last post by:
Hello I wondered if this script seemed correct? The user should enter a userid and password in two text fields, and then pass these values to the method login_user. Could I ask, is there any approach to, once the user clicks the submit button to test the userid and password has been entered, and the text fields are not empty.
14
12341
by: Christopher Benson-Manica | last post by:
I'm trying to check whether a string is all digits. This part is easy: function allDigits( str ) { var foo=str.split( '' ); // better than charAt()? for( var idx=0; idx < foo.length; idx++ ) { if( !isDigit(foo) ) { return false; } }
12
2491
by: Vicky | last post by:
What is the better way of checking blank string is it strvar.lenght > 0 or strvar == "" or something else Thnaks
6
15547
by: dwight | last post by:
I have a string that can't contain the following characters: ../\|}{[:;+=_)(*&^%$#@!~` What would be easiest way to check the string? Should I use regexp or create a array with these characters and compare each character of string to the array? Thanks
8
3165
by: J-P-W | last post by:
Hi, anyone got any thoughts on this problem? I have sales reps. that remotely send their data to an ftp server. The office downloads all files, the code creates an empty file, then downloads the data to it, then moves on to the next rep: ---------- conTARGETVisits = FTPLocation & "RepCallPlan" & Format(MyRepCode, "00")
42
2803
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and there is no characters in the variable it does not enter my IF statement like I would think it should. How do I properly check for a
3
1316
by: Nader | last post by:
Hello, I read some files name from a directory and then I put these name in a list. I will check whether it is empty or not, and I would do it with an exception. With if statement it is very simple: If list_of_files != "" : # this can be if list_of_files != : get the files elas:
5
3628
by: bob | last post by:
Hi, i want to check whether the textbox of the detailsview is not left empty, in insert mode. I did this: Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting Dim h As String
1
1836
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
In a RowDataBound event, I want to check if a column is empty (it's a simple column, not a template with a label in it). I use the following syntax to get the value from the cell: s = e.row.cells(1).text but s returns " " so I can't use String.IsNullOrEmpty(s) function, because it's not empty. What can I do?
0
9000
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
9396
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
9339
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
8260
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
6804
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
6081
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
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
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.