Hi. I would like to convert empty string "" to zero in C#. The data is stored in (string) arrays. There is jagged array which contains all 5 string arrays. Could someone help?

// Jagged array intialization
string[][] all_prices = new string[5][];
all_prices[0] = nordpool;
all_prices[1] = tvh;
all_prices[2] = ttoist;
all_prices[3] = tarj1v;
all_prices[4] = tarj2v;

There can be some empty ("") values and I want convert them to zero.

Edited 4 Years Ago by Metobu: I added code

What exactly do you mean? Something like this?

string.IsNullOrEmpty(all_prices[0][0]) ? 0 : int.Parse(all_prices[0][0]);

That example just returns 0 if it's null or empty, otherwise it parses the string (you could use float/double/decimal instead), but I'm not sure if that's what you're trying to do... If it is, could I ask why numeric values are being stored as strings?

I am using streamreader and first I read a line to string array, then split values separated by : and later I make decimal type list and add prices to list. For example, there can be line like this:

:2.20:2.20:2.20:2.20|7.18:7.18:7.18:7.18:7.18|6.95:6.95:6.95:6.95:6.95|6.98:6.98:6.98:6.98:6.98|6.86:6.86:6.86:6.86:6.86

First value is "" and I would like to convert its value to zero. Now I get "formatexception" when running the program. I don't know how to make a loop which check "" values and convert them.

Ah, ok. So you would use something similar to what I put above.

for(...)
{
    ...
    double d = all_prices[i1][i2] == string.Empty ? 0.0 : double.Parse(all_prices[i1][i2]);
    ...
}

The ? and : operators are just shorthand for:

for(...)
{
    ...
    double d;
    if(all_prices[i1][i2] == string.Empty)
        d = 0.0;
    else
        d = double.Parse(all_prices[i1][i2]);
    ...
}

You could use either one, it doesn't really matter. Also, string.Empty is equivalent to "", it doesn't really matter which one you use. Without your source code I can't really make any specific recommendations, but you should be able to use this as a starting point.

I don't know what I should do with d variable. Here is more code...

                using (StreamReader reader = new StreamReader(e.Result))
                {
                    List<string> line = new List<string>();

                    while (!reader.EndOfStream)
                    {
                        line.Add(reader.ReadLine());
                        //MessageBox.Show(line[0]);
                    }

                    reader.Close();

                    string[] section = line[0].Split('|');
                    string[] nordpool = section[0].Split(':');
                    string[] tvh = section[1].Split(':');
                    string[] ttoist = section[2].Split(':');
                    string[] tarj1v = section[3].Split(':');
                    string[] tarj2v = section[4].Split(':');

                    // Jagged array
                    string[][] all_prices = new string[5][];
                    all_prices[0] = nordpool;
                    all_prices[1] = tvh;
                    all_prices[2] = ttoist;
                    all_prices[3] = tarj1v;
                    all_prices[4] = tarj2v;

                    for (int i = 0; i < all_prices.Count(); i++)
                    {
                        if (all_prices[i].Contains(""))
                        {
                            // Dont know what to do
                        }
                    }

                    List<double> list_nordpool = new List<double>();
                    List<double> list_tvh = new List<double>();
                    List<double> list_ttoist = new List<double>();
                    List<double> list_tarj1v = new List<double>();
                    List<double> list_tarj2v = new List<double>();

                    for (int j = 0; j < number; j++)
                    {
                        list_nordpool.Add(double.Parse(all_prices[0][j]));
                        list_tvh.Add(double.Parse(all_prices[1][j]));
                        list_ttoist.Add(double.Parse(all_prices[2][j]));
                        list_tarj1v.Add(double.Parse(all_prices[3][j]));
                        list_tarj2v.Add(double.Parse(all_prices[4][j]));
                    }

                    ...

Change this:

for (int j = 0; j < number; j++)
{
    list_nordpool.Add(double.Parse(all_prices[0][j]));
    list_tvh.Add(double.Parse(all_prices[1][j]));
    list_ttoist.Add(double.Parse(all_prices[2][j]));
    list_tarj1v.Add(double.Parse(all_prices[3][j]));
    list_tarj2v.Add(double.Parse(all_prices[4][j]));
}

to this:

for (int j = 0; j < number; j++)
{
    list_nordpool.Add(all_prices[0][j] == "" ? 0.0 : double.Parse(all_prices[0][j]));
    list_tvh.Add(all_prices[1][j] == "" ? 0.0 : double.Parse(all_prices[1][j]));
    list_ttoist.Add(all_prices[2][j] == "" ? 0.0 : double.Parse(all_prices[2][j]));
    list_tarj1v.Add(all_prices[3][j] == "" ? 0.0 : double.Parse(all_prices[3][j]));
    list_tarj2v.Add(all_prices[4][j] == "" ? 0.0 : double.Parse(all_prices[4][j]));
}

This compares the string to the empty string. If they are equal, it passes 0 into the Add() method. Otherwise, it passes the return value from double.Parse() (but does not execute Parse() unless it has to).

This article has been dead for over six months. Start a new discussion instead.