By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Empty String Question

P: n/a
I have an array of of 5 string elements. I put values in 3 of them. Yet
when I loop over them, I do not catch the empty string. The code output
below does not include "Empty"
string[]x=new String[5];
x[0]="111";
x[1]="222";
x[2]="333";

string msg="";
for(int i=0;i<x.Length;i++)
{
if(x[i]==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x[i]+"\n";
}
}

MessageBox.Show(msg);
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,
I have an array of of 5 string elements. I put values in 3 of them. Yet
when I loop over them, I do not catch the empty string. The code output
below does not include "Empty"

Your string equals String.Empty when its value is "".
In your code sample, your string doesn't have any value,
so you have to compare it with null:

if(x[i]==null)
{
msg+=msg+"Empty\n";
}
---------------------------

string[]x=new String[5];
x[0]="111";
x[1]="222";
x[2]="333";
x[3]="";
x[4]="";

string msg="";
for(int i=0;i<x.Length;i++)
{
if(x[i]==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x[i]+"\n";
}
}

MessageBox.Show(msg);
Nov 16 '05 #2

P: n/a
Or, as may be more helpful depending on the types of conditions you're
trying to catch:

if(x[i] == null || x[i] == String.Empty)
"Denny Britz" <zo***@web.de> wrote in message
news:ei****************@TK2MSFTNGP14.phx.gbl...
Hi,
I have an array of of 5 string elements. I put values in 3 of them. Yet
when I loop over them, I do not catch the empty string. The code output
below does not include "Empty"

Your string equals String.Empty when its value is "".
In your code sample, your string doesn't have any value,
so you have to compare it with null:

if(x[i]==null)
{
msg+=msg+"Empty\n";
}
---------------------------

string[]x=new String[5];
x[0]="111";
x[1]="222";
x[2]="333";
x[3]="";
x[4]="";

string msg="";
for(int i=0;i<x.Length;i++)
{
if(x[i]==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x[i]+"\n";
}
}

MessageBox.Show(msg);

Nov 16 '05 #3

P: n/a
.NET 2.0 makes this kind of comparison very easy. There is a new static
method on the String class called "IsNullOrEmpty" which returns true if the
string passed to it is null or empty.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marcos Stefanakopolus" <ta*******@hotmail.com> wrote in message
news:42********@news.microsoft.com...
Or, as may be more helpful depending on the types of conditions you're
trying to catch:

if(x[i] == null || x[i] == String.Empty)
"Denny Britz" <zo***@web.de> wrote in message
news:ei****************@TK2MSFTNGP14.phx.gbl...
Hi,
I have an array of of 5 string elements. I put values in 3 of them.
Yet
when I loop over them, I do not catch the empty string. The code output
below does not include "Empty"

Your string equals String.Empty when its value is "".
In your code sample, your string doesn't have any value,
so you have to compare it with null:

if(x[i]==null)
{
msg+=msg+"Empty\n";
}
---------------------------

string[]x=new String[5];
x[0]="111";
x[1]="222";
x[2]="333";
x[3]="";
x[4]="";

string msg="";
for(int i=0;i<x.Length;i++)
{
if(x[i]==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x[i]+"\n";
}
}

MessageBox.Show(msg);


Nov 16 '05 #4

P: n/a
Thanx all.

Refreshing my C# skills. It's been 2 years since I've used tham and I am
rusty. So if I annoy anyone with simplistic questions, I appologize in
advance.

The answers to this one is embarassing. I should have known.
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.