Andy
Andy

Reputation: 89

Catch null values before adding to a string

Hey guys, I can only apologise for asking such a noob question, but this is the second time today I'm going to be relying on this awesome forum :)

I have a string that is made up of values from an array, taken from an excel sheet, only some of these values are blank. I'm outputting these values to a CSV file, but don't want the blank values to output, these values are outputting as commas, and so are giving me extra lines in my CSV file.

Anyway, here's my code, I want to compare the array values separately to not equal "" before I add them to the string. If this is not possible, I am thinking that I could split the string, compare, and then rejoin it.

enter code here//Get a range of data. 

range = objSheet.get_Range(ranges1, ranges2);

//Retrieve the data from the range. 
Object[,] saRet;
saRet = (System.Object[,])range.get_Value(Missing.Value);

//Determine the dimensions of the array. 
long iRows;
long iCols;
iRows = saRet.GetUpperBound(0);
iCols = saRet.GetUpperBound(1);

//Build a string that contains the data of the array. 
String valueString;
valueString = "";

System.IO.StreamWriter OutWrite = new System.IO.StreamWriter("h:\\out.csv");

    for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
    {

        for (long colCounter = 1; colCounter <= iCols; colCounter++)
        {
            //Write the next value into the string.
            valueString = String.Concat(valueString, Convert.ToString(saRet[rowCounter, colCounter]) + ", ");
        }

        //Write in a new line. 
        {
            valueString = String.Concat(valueString, "\n");
        }
    }

    //Report the value of the array.

    MessageBox.Show(valueString, "Array Values");

    OutWrite.WriteLine(valueString);
    OutWrite.Close();
    OutWrite.Dispose();
}

Cheers, and thanks for your patience!

Andy.

Upvotes: 1

Views: 889

Answers (4)

Andrew Lewis
Andrew Lewis

Reputation: 5256

newValue += (oldValue ?? string.empty)

Upvotes: 0

Saeed Amiri
Saeed Amiri

Reputation: 22555

 public static class ExtensionLib
 {

        public static string MyConcat(this string input, params string[] others)
        {
            if (others == null || others.Length == 0)
               return input;
            string retVal = input??"";
            foreach(string str in others)
            {
                if (String.IsNullOrEmpty(str))
                    return input;
                retVal += str;
            }
            return retVal;
        }
 }

and use it as bellow:

....

valueString.MyConcat(arg1, arg2, ...);

Take a care about this line:

valueString = String.Concat(valueString, Convert.ToString(saRet[rowCounter, colCounter]) + ", ");

instead write

valueString = valueString.MyConcat(Convert.ToString(saRet[rowCounter, colCounter]), ", ");

Upvotes: 2

user203570
user203570

Reputation:

String valueString;
valueString = "";
if (!valueString.Equals(compare))

You can just rewrite this as:

if (!String.IsNullOrEmpty(compare))

In fact, you should. No need to reinvent what .NET provides.

Upvotes: 1

CD..
CD..

Reputation: 74096

I think you are looking for String.IsNullOrEmpty Method .

(Or String.IsNullOrWhiteSpace Method in .Net 4)

Upvotes: 4

Related Questions