奇怪的IF条件,不知道哪里出错了 下断点调试file[i]data1.Rows[k].Cells[3].ValuecomboBox2.Text这三个变量究竟是什么 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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].ValuecomboBox2.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 显然是格式的问题:if(xxx!=null) xxx.ToString();然后xxx.trim()去空格再比较 这种代码,调试起来可就费劲了你应该把data1.Rows[k].Cells[3].Value.ToString()先放到一个string型的局部变量里然后if条件里判断它这样一步一步断点跟,才好看出到底哪里不对而且你应该在ToString之前判断这个Value不是null 还有,Replace之前,根本没必要判断是否包含反正不包含,也不会有字符串被替换掉 还有,替换之前,要先判断data1.Rows[k].Cells[2].Value.ToString()不为String.Empty你把空字符串替换成别的,是会出错的 你最好分开写适合别人读也适合调试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 你没仔细看……这不是单纯的replace,是要满足三个包含或不包含条件才去replace 在DATASET中怎么调用新建好的insert类型的查询? 求助wpf中的数据绑定的问题 窗体程序只运行一次,第二次启动激活该窗体的问题 求助:关于button按钮! WPF如何设置treeview的选中项? 关于摄像头 懂VC++ && C# 的高手进 如何利用webBrowser得到meta元素?求救高手 怎么样从数据库中取出数据放入模板呢?请高手指点!急!!!! IIS浏览页面,出现 “无法访问已关闭的资源集。 ” 给50分!!!!谢了! Winform 中打开 PDF文件 wince webservice 无法从传输连接中读取数据
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
你应该把data1.Rows[k].Cells[3].Value.ToString()先放到一个string型的局部变量里
然后if条件里判断它
这样一步一步断点跟,才好看出到底哪里不对
而且你应该在ToString之前判断这个Value不是null
反正不包含,也不会有字符串被替换掉
你把空字符串替换成别的,是会出错的
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