!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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
AndyAinscowFreelance programmer / ConsultantCommented:
String s2 = ",,,";
String s3 = s2.Trim(',');
//s3 is now ""

(Replace didn't work with an empty char)
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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
DivinityCommented:
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 ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi please use following
string.IsNullOrWhiteSpace

as

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

thanks
0
AndyAinscowFreelance 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.