!string.IsNullOrEmpty c# Problem VS2010

In the code below i am trying to check that the string does not contain "" before processing.

some how this is still processing the string when it contains ""

the data is picked up from a comma delimited array, with blank ,,,,,, where there is no data, how can i stop the processing of stirngs that contain no data

Thank you
if (!string.IsNullOrEmpty(Charon))
                {
                    shipsRetrieved = shipsRetrieved + Charon + "<br />".ToString();
                }

Open in new window

LVL 3
QuinnDesterAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
Are you saying that you want to test for "not ,,,,,," instead of not IsNullOrEmpty?

if (!string.IsNullOrEmpty(Charon) && (Charon != ",,,,,,"))
                {
                    shipsRetrieved = shipsRetrieved + Charon + "<br />".ToString();
                }
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Make a copy of the string, then remove all commas.  Now test if this string is empty.  (Remember to do this on a copy because it will trash the real line)
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
String s2 = ",,,";
String s3 = s2.Trim(',');
//s3 is now ""

(Replace didn't work with an empty char)
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
QuinnDesterAuthor Commented:
the string is "" no commas in it, the data is collected from a comma delimted array, the ,,,,,, are just delimitations as place holders so the correct data is alway in the right place in the array.

i only mentioned this so you know how the data is picked up, incase that has a bearing on what is held in the string.
0
 
DivinityConnect With a Mentor Commented:
While it is strange that an empty string does go through your if-statement somehow, you could avoid the issue altogether by doing some like this:

string text = "abc,def,,ghi";
string[] parts = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

Open in new window


This ofcourse assumes you are using creating your string array from a longer string which contains all the entries, separated by a comma. The StringSplitOptions.RemoveEmptyEntries will automatically remove the empty entries from the array, so in the example above the array will contain 3 non-empty entries, in stead of 4 entries, one of which would be empty.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
"" is an empty string.  As Divinity says that should be trapped and not processed.

Please paste your complete code - you might be missing something else and thinking the failure is with this bit of code.
0
 
Imran Javed ZiaConnect With a Mentor Consultant Software Engineer - .NET ArchitectCommented:
Hi please use following
string.IsNullOrWhiteSpace

as

 if (!string.IsNullOrWhiteSpace(Charon))  
                {  
                    shipsRetrieved = shipsRetrieved + Charon + "<br />".ToString();  
                }

thanks
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
ps.  Are you certain it is "" and not " " (or variant thereof)
0
 
QuinnDesterAuthor Commented:
My mistake, i was checking for an empty string after adding additional text to the string, so they were never empty...

moving the check to earltier in the code sorted it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.