[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to remove null values from string array in my C# .net window application??

Posted on 2011-02-16
3
Medium Priority
?
1,046 Views
Last Modified: 2012-05-11
i have created one dimensional string array ...
for ex.
String[] QuetionOptions = new String[6];
QuetionOptions[0] = null
QuetionOptions[1] = null
QuetionOptions[2] = "option"
QuetionOptions[3] = null
QuetionOptions[4] = "option"
QuetionOptions[5] = null
QuetionOptions[6] = "option"

how can i remove null values from above string array ??
and store it into new string array ....
0
Comment
Question by:Parth48
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34905858
If LINQ is an option you could use:
            String[] filtered = (from item in QuetionOptions.AsQueryable()
                                 where item != null
                                 select item).ToArray();

Open in new window

0
 
LVL 7

Accepted Solution

by:
mkobrin earned 2000 total points
ID: 34905941
if linq is not an option then do the following
List<string> result = new List<string>
for(int j = 0; j < QuetionOptions.length; j++)
{
    if(QuetionOptions[j] != null)
        result.add(QuetionOptions[ji]);
}
//you now have a list with no nulls if you specifcally want an array then do the following as well:
string[] resultArray = new string[result.count]
int j = 0;
foreach(string data in result)
{
    resultArray[j] = data ;
    j++;
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question