473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

better way of checking for blank string

What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else

Thnaks
Nov 16 '05 #1
12 2463
Vicky <vi***@hh.com> wrote:
What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else


Either of those will work (typos aside - the property is Length, and
you should be checking for it being 0, not being greater than 0). You
may find the latter more readable. The former is likely to be slightly
more efficient, but frankly it's not going to amount to a great
difference in most cases.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
You can also compare it to string.Empt
Nov 16 '05 #3
Vicky wrote:

What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else

Thnaks


I use:

strvar == string.Empty

for no particular reason.
Nov 16 '05 #4
Vicky wrote:
What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else


The first: strvar.lenght > 0
Nov 16 '05 #5
Julie wrote:
Vicky wrote:
What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else

Thnaks

I use:

strvar == string.Empty

for no particular reason.


Personally, I always use:

if (String.Empty.Equals(strvar))
{
...
}

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #6
> >>What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else

Thnaks

I use:

strvar == string.Empty

for no particular reason.


Personally, I always use:

if (String.Empty.Equals(strvar))
{
...
}

*lol*

Why not simply

if (System.String.Compare(System.String.Empty, strval, false) == 0)
{
....
}

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #7
It's better performance wise to check if the Length is 0

--
Jared Parson [MSFT]
ja******@online.microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:ek*************@TK2MSFTNGP10.phx.gbl...
Julie wrote:
Vicky wrote:
What is the better way of checking blank string is it
strvar.lenght > 0
or

strvar == ""

or something else

Thnaks

I use:

strvar == string.Empty

for no particular reason.


Personally, I always use:

if (String.Empty.Equals(strvar))
{
...
}

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk

Nov 16 '05 #8
I think the jitter will always optimize s == "" to s.Length == 0 anyway.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Jared Parsons [MSFT]" <ja******@online.microsoft.com> schrieb im
Newsbeitrag news:Oj*************@TK2MSFTNGP09.phx.gbl...
It's better performance wise to check if the Length is 0

--
Jared Parson [MSFT]
ja******@online.microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in message news:ek*************@TK2MSFTNGP10.phx.gbl...
Julie wrote:
Vicky wrote:

>What is the better way of checking blank string is it
>strvar.lenght > 0
>or
>
>strvar == ""
>
>or something else
>
>Thnaks
I use:

strvar == string.Empty

for no particular reason.


Personally, I always use:

if (String.Empty.Equals(strvar))
{
...
}

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk


Nov 16 '05 #9
cody <pl*************************@gmx.de> wrote:
I think the jitter will always optimize s == "" to s.Length == 0
anyway.


I don't think so - for a start, it would have to add in a nullity test.

It's not terribly easy to pick out from a disassembly of optimised
native code, but I'm pretty sure it's *not* making that optimisation.

The following test program seems to bear that out. Note how many
iterations are needed to see a really significant difference though...

using System;

class Test
{
const int Iterations = 1000000000;
static void Main()
{
{
DateTime start = DateTime.Now;
int x = 0;
for (int i=0; i < Iterations; i++)
{
x += Bar("");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} {1}", x, end-start);
}

{
DateTime start = DateTime.Now;
int x = 0;
for (int i=0; i < Iterations; i++)
{
x += Foo("");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} {1}", x, end-start);
}
}

static int Foo (string x)
{
if (x=="")
{
return 1;
}
return 0;
}

static int Bar (string x)
{
if (x.Length==0)
{
return 1;
}
return 0;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
> > I think the jitter will always optimize s == "" to s.Length == 0
anyway.


I don't think so - for a start, it would have to add in a nullity test.

It's not terribly easy to pick out from a disassembly of optimised
native code, but I'm pretty sure it's *not* making that optimisation.

The following test program seems to bear that out. Note how many
iterations are needed to see a really significant difference though...

using System;

class Test
{
const int Iterations = 1000000000;
static void Main()
{
{
DateTime start = DateTime.Now;
int x = 0;
for (int i=0; i < Iterations; i++)
{
x += Bar("");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} {1}", x, end-start);
}

{
DateTime start = DateTime.Now;
int x = 0;
for (int i=0; i < Iterations; i++)
{
x += Foo("");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} {1}", x, end-start);
}
}

static int Foo (string x)
{
if (x=="")
{
return 1;
}
return 0;
}

static int Bar (string x)
{
if (x.Length==0)
{
return 1;
}
return 0;
}
}

operator== internally calls string.Equals which certainly will return true
if (s1.Lenght==0l && s.Length==0) first, so your test has very little value
since you do a method call too.

But if the jitter would optimize s=="" to s!=null && s.Lenght==0 we can
certainly expect a noticable performance improvement.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #11
cody <pl*************************@gmx.de> wrote:
operator== internally calls string.Equals which certainly will return true
if (s1.Lenght==0l && s.Length==0) first, so your test has very little value
since you do a method call too.
Why does it have very little value? It shows that

if (s=="")

isn't as fast a test as

if (s.Length==0)

which was what was being tested. Don't forget that .Length is
effectively a method call too, as Length is a property.
But if the jitter would optimize s=="" to s!=null && s.Lenght==0 we can
certainly expect a noticable performance improvement.


*If* it did, yes - but there's no evidence that it *does* do that
optimisation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #12
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
cody <pl*************************@gmx.de> wrote:
operator== internally calls string.Equals which certainly will return true if (s1.Lenght==0l && s.Length==0) first, so your test has very little value since you do a method call too.


Why does it have very little value? It shows that

if (s=="")

isn't as fast a test as

if (s.Length==0)

which was what was being tested. Don't forget that .Length is
effectively a method call too, as Length is a property.
But if the jitter would optimize s=="" to s!=null && s.Lenght==0 we can
certainly expect a noticable performance improvement.


*If* it did, yes - but there's no evidence that it *does* do that
optimisation.


It would seem to me that s.Length is readily available directly (as it is
stored *somewhere*), whereas a string comparison would need more processing
(via some internal algorithm) even in the trivial "" condition. Besides, MS
may not have optimized s=="" since s.Length==0 is available.

--
Mabden
Nov 16 '05 #13

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

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
3
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed...
4
by: Himanshu Singh Chauhan | last post by:
Hello all! I am a newbie in C world. I was making a program from K&R (1-17). I made 2 functions and found that same problem could be solved with 1. Making new varibles (array, 2 or more). 2....
8
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex...
42
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...
6
by: Jeff | last post by:
Could someone tell me the easiest way to check a string of variable length to see if it consists of all blank characters? ....or perhaps more generally, to see if all of the characters are the...
13
by: pvong | last post by:
VB.Net I'm pulling Data from a DB and creating a letter by filling the Labels. It works perfectly, but if Addr2 and Addr3 are blank, it leaves a Blank space in that area. Is there a way to...
7
by: elnoire | last post by:
Greetings! I've just started learning python, so this is probably one of those obvious questions newbies ask. Is there any way in python to check if a text file is blank? What I've tried to...
4
by: barmatt80 | last post by:
I have created an unbound form so that when users enter the information, there is a save button they have to click, checking to make sure a field(txtDocketNo) is not blank. For some reason I cannot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.