字符串数组索引不包含空值

本文关键字:包含 空值 索引 数组 字符串 | 更新日期: 2023-09-27 18:03:09

我有一个字符串数组,但是空值没有索引和存储

例如,如果我有一个数组{"one", ", "three"},数组[0]的值是1,数组[1]的值变成3而不是",为什么空值被删除,下一个值的索引变成空值

这是我的代码,我使用Open XML SDK来读取和存储excel单元格到一个数组。

foreach (var row in rows)
{
        int i = 0;
        string[] rowData = new string[9];
        var cells = row.Elements<Cell>();
        foreach (var cell in cells)
        {
            if (cell != null)
            {
                var index = Int32.Parse(cell.CellValue.Text);
                if (cell.DataType != null)
                {
                    rowData[i] = GetSharedStringItemById(workbookPart, index);
                }
            }
            i++;
        }
        var store = new UserStore<User>(context);
        var manager = new UserManager<User>(store);
        var user = new User
        {
            UserName = rowData[0],
            FirstName = rowData[1],
            LastName = rowData[2],
            Email = rowData[3],
            EmailConfirmed = true,
            RegisterdOn = now,
            Employer = new Employer()
            {
                CompanyName = rowData[4],
                AddressLine1 = rowData[5],
                AddressLine2 = rowData[6],
                City = rowData[7],  <---if value of city is null, the value of PostCode is used?
                PostCode = rowData[8],
            }
        };
        var result = manager.Create(user, "123456");
        if (result.Succeeded)
        {
            manager.AddToRole(user.Id, "Employer");
        }
}

字符串数组索引不包含空值

如果有帮助的话,你可以看看下面的

//在转换为整数之前检查cell的值是否为null

if(cell.CellValue.Text != null){
var index = Int32.Parse(cell.CellValue.Text);

//将转换值添加到数组

rowData[i] = GetSharedStringItemById(workbookPart, index);
  } 
//in case the value contained in a cell is null, then assign either "" or null 
rowData[i] = "";

还要注意"不等于null。如果测试"和null是否相等,测试结果将为false。