【问题标题】:How to Split Array that is Declared Global如何拆分声明为全局的数组
【发布时间】:2013-10-29 13:55:48
【问题描述】:

我有一个由 First Name _ Last Name 组成的数组,因此它们会像这样读取 迈克尔·乔丹 哈维尔_洛佩兹 乔治_琼斯

我有一个循环设置来遍历其中的每一个,但我只想获取“”之后的内容我遇到的问题是该数组是全局声明的,并且它被声明为远很多地方让我改变。如果我尝试使用 .Split 函数,我会收到 System.Array 不包含拆分定义的错误。获取数组中“”之后的数据的另一种选择是什么?

public static string GetEmployees()
{
    string queryString = "select employeeName from tbl_GlobalEmployeeData where state = 'AL';
    SqlConnection connection = new SqlConnection(Connection.MyConnectionString.ConnectionStrings[0]);
    {
        SqlCommand cmd = new SqlCommand(queryString, connection);
        connection.Open();
        List<string> tempList = new List<string>();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            try
            {
                if (!reader.IsDBNull(0))
                {
                    tempList.Add(reader[0].ToString() + "_" + reader[1].ToString());
                }
            }
            catch
            {
                if (!reader.IsDBNull(0))
                {
                    tempList.Add(reader[0].ToString() + "_" + reader[1].ToString());
                }
            }
        }
        reader.Close();
        AllCompanyEmployees.State.ThisStore = tempList.ToArray();
        for (int q = AllCompanyEmployees.State.ThisStore.GetLowerBound(0); q <= AllCompanyEmployees.State.ThisStore.GetUpperBound(0); q++)
        {
            return AllCompanyEmployees.State.ThisStore[q];
        }
        return null;
    }
}

}

for (int q = AllCompanyEmployees.State.ThisStore.GetLowerBound(0); q <= AllCompanyEmployees.State.ThisStore.GetUpperBound(0); q++)
{
   //This line is where I get the error mentioned above
   string lastName = AllCompanyEmployees.State.ThisStore.Split('_')[1];
}

【问题讨论】:

  • 我看不到您的问题与代码之间的联系。你是不是贴错了sn-p?
  • "" 之后?一无所有之后?你在说什么?你的意思是在下划线上分开吗?请显示您的全局数组的代码,并阐明您要对示例执行的操作。此外,您不能在数组上使用Split。这是String 的一种方法。您需要在数组中的各个字符串上使用 Split
  • 向试图提供帮助的 2 人道歉,我确实发布了错误的代码 sn-p。请查看我的编辑中发布的代码。
  • 还有很多未解决的问题。在您回复之前无法回答您的问题。
  • 我想拆分数组 - 例如它读取 Javier_Lopez 我想从数组中取出 Lopez。

标签: c# arrays split


【解决方案1】:

我认为您的问题是“我想拆分数组 - 例如它读取 Javier_Lopez 我想从数组中取出 Lopez”

很简单:

string last = yourString.Split(new char[] { '_' })[1];

再次,您似乎在数组上使用它,这就是您收到该错误的原因。您需要遍历您的数组并对数组中的每个单独的字符串执行此操作。

编辑:要修改数组并只保留姓氏,试试这个:

int i = 0;
foreach (string s in stringArray)
{
    stringArray[i] = stringArray[i].Split(new char[] { '_' })[1];
    i++;
}

【讨论】:

  • 如何对数组中返回的每个名称执行此操作?我按照你说的去做,只是没有掌握如何去做。
【解决方案2】:

您只能在字符串上使用Split。所以你可以这样做:

List<string> lastNames = new List<string>();
for (int q = AllCompanyEmployees.State.ThisStore.GetLowerBound(0); q <= AllCompanyEmployees.State.ThisStore.GetUpperBound(0); q++)
{
   string lastName = AllCompanyEmployees.State.ThisStore[q].Split('_')[1];
   lastNames.Add(lastName);
}

最后,您将获得一个 List&lt;string&gt;,其中包含您员工的所有姓氏。这样你就可以继续工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多