views:

99

answers:

4

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.

+4  A: 

I think you are looking for String.IsNullOrEmpty Method .

(Or String.IsNullOrWhiteSpace Method in .Net 4)

CD
+1  A: 
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.

Matthew Ferreira
I can only apologise, that if was not meant to be in the code that I posted. I was only testing with that :)
Andy
Not quite sure what you're asking then. You can use String.IsNullOrEmpty for exactly the purpose you described. Also, I notice that you have extra { } at your comment 'Write in a new line'. EDIT: never mind about the extra { }, your code was edited.
Matthew Ferreira
You may also want to consider not using '\n' in favor of Environment.NewLine so that you handle new lines on all platforms.
Matthew Ferreira
Well, valueString is (as far as I understand), a join of all of the values in the array, so essentially, valueString doesn't actually contain a null at this stage. Please correct me if I'm wrong!
Andy
If you're leaving valueString as empty, you don't need to declare a variable to hold it unless you're going to use it in multiple places. `"".Equals(compare)` will work just as well.
Joel Etherton
+2  A: 
 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]), ", ");
SaeedAlg
Thats gotten me a lot closer SaeedAlg, but I'm a bit confused about this line: valueString.MyConcat(arg1, arg2, ...); Cheers!
Andy
see http://msdn.microsoft.com/en-us/library/w5zay9db%28VS.71%29.aspx for understanding params, and for extension methods see http://msdn.microsoft.com/en-us/library/bb383977.aspx, I'd edited the code get the current code :D
SaeedAlg
for example you can do this: valueString = valueString.MyConcat("Hello", ",", "saeed", "\n");
SaeedAlg
Thank you very much! I'm going to accept this as my answer. Thanks to everyone who contributed! You've been a great help!
Andy
A: 
newValue += (oldValue ?? string.empty)
Andrew Lewis