Empty String Question

  • Thread starter Thread starter web1110
  • Start date Start date
W

web1110

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==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x+"\n";
}
}

MessageBox.Show(msg);
 
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==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==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x+"\n";
}
}

MessageBox.Show(msg);
 
Or, as may be more helpful depending on the types of conditions you're
trying to catch:

if(x == null || x == String.Empty)


Denny Britz said:
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==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==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x+"\n";
}
}

MessageBox.Show(msg);
 
.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]
- (e-mail address removed)

Marcos Stefanakopolus said:
Or, as may be more helpful depending on the types of conditions you're
trying to catch:

if(x == null || x == String.Empty)


Denny Britz said:
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==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==String.Empty)
{
msg+=msg+"Empty\n";
}
else
{
msg=msg+x+"\n";
}
}

MessageBox.Show(msg);

 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top