473,396 Members | 1,834 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,396 software developers and data experts.

Empty String Question

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
4 2330
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
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
.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris Connett | last post by:
I have an interesting problem with eval(). ---Background Info--- The program I'm working on launches a separate process with a popen to do some highly specialized processing of input, then this...
2
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, This looks like a long mail, but at the end of this post is my socket wrapper attached. I want to make a timeout procedure that starts counting down after the...
21
by: M D | last post by:
You know how you assume you know until you find out you don't know. Well, I typed into a function definition "..., new String("")). I know what I want. Everyone reading this knows what I want....
8
by: Ottar | last post by:
I have a few numeric fields, and when I update i get the error: "Input string was not in a correct format". Next line:" System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&...
14
by: cj | last post by:
What is string.empty used for? I can't say: if string.empty then I have to use: if string = "" then which is ok, I just want to know what .empty is for.
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
10
by: mr_sorcerer | last post by:
Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn't check null pointers?
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
21
by: Sami | last post by:
string = "" or string = string.Empty? should is the proper way? Sami
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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,...

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.