Search code examples
c#epplus

String from excel having length size different than real value


I built an web-app that reading string value from uploaded excel file row by row using EPPLUS.

Just now I am encountered strange behaviour, my string.length size is different than it's real value.

code to make it clear :

//pretend str value = "cat"
string str = Worksheets.Cells["A1"].Value.ToString().Trim();

//output will be = "cat"
Debug.Writeline(str);

//it should be = 3 right
//but it's actually = 4
Debug.Writeline(str.Length);

can anyone explain why this is can happen ? and how to solve it, because I need to compare that string with my own hardcoded string


Solution

  • If the length is reporting as 4 then there's certainly something else in your string. Have you taken a look at it byte-by-byte?

    Try this:

    var cat = Worksheets.Cells["A1"].Value.ToString().Trim();
    foreach (var c in cat.ToCharArray())
    {
        Debug.WriteLine("{0}: '{1}'", (int)c, c);
    }
    

    It should help you determine what else is hidden in your string. I doubt that this is something "odd" that EPPlus is doing (I use it all the time and have never experienced anything like this) - it's almost certainly something strange in your source data.