下断点调试
file[i]
data1.Rows[k].Cells[3].Value
comboBox2.Text
这三个变量究竟是什么

解决方案 »

  1.   


    string[] file = File.ReadAllLines(data1.Rows[k].Cells[5].Value.ToString());
                    for (int i = 0; i < file.Length; i++)
                    {
                        newfile.Add(file[i]);
                        if (data1.Rows[k].Cells[2].Value.ToString() != comboBox1.Text)
                        {
                            if (file[i].Contains(data1.Rows[k].Cells[2].Value.ToString()) && data1.Rows[k].Cells[2].Value.ToString() != comboBox1.Text + "0")
                            {
                                file[i] = file[k].Replace(data1.Rows[k].Cells[2].Value.ToString(), comboBox1.Text);
                            }
                        }
                        if (data1.Rows[k].Cells[3].Value.ToString() != comboBox2.Text)
                        {
                            if (file[i].Contains(data1.Rows[k].Cells[3].Value.ToString()) && data1.Rows[k].Cells[3].Value.ToString() != comboBox2.Text + "0")
                            {
                                file[i] = file[i].Replace(data1.Rows[k].Cells[3].Value.ToString(), comboBox2.Text);
                            }
                        }
                    }
    这样应该就能看明白了吧
    data1.Rows[k].Cells[3].Value
    comboBox2.Text
    这两个分别是两个数值,如果取到的一行中包含data1.Rows[k].Cells[3].Value,并且data1.Rows[k].Cells[3].Value跟comboBox2.Text不相等,那么就用comboBox2.Text替换掉file[i]中的data1.Rows[k].Cells[3].Value
      

  2.   

    显然是格式的问题:if(xxx!=null) xxx.ToString();然后xxx.trim()去空格再比较
      

  3.   

    这种代码,调试起来可就费劲了
    你应该把data1.Rows[k].Cells[3].Value.ToString()先放到一个string型的局部变量里
    然后if条件里判断它
    这样一步一步断点跟,才好看出到底哪里不对
    而且你应该在ToString之前判断这个Value不是null
      

  4.   

    还有,Replace之前,根本没必要判断是否包含
    反正不包含,也不会有字符串被替换掉
      

  5.   

    还有,替换之前,要先判断data1.Rows[k].Cells[2].Value.ToString()不为String.Empty
    你把空字符串替换成别的,是会出错的
      

  6.   

    你最好分开写适合别人读也适合调试
    if(data1.Rows!=null)
    {
       Row row=data1.Rows[k];
        object obj=row.Cells[3].Value
       if(obj!=null&&file.Lenth>=i)
        {
                string strFile=file[i];
                string strObj=obj.ToString();
                 if strFile.Contains(strObj) && strFile != comboBox2.Text )
                            {
                                file[i] =strFile.Replace(strObj, comboBox2.Text);
                            }
        }
    }
    4
      

  7.   

    你没仔细看……这不是单纯的replace,是要满足三个包含或不包含条件才去replace