运算符'!='不能应用于'string []'和'string'类型的操作数(是的我之前已经问过类似的问题,我看过一个,但是使用的上下文/元素是不同类型的,所以我'我很感激帮助我的情况:))
我有一个文件,我想在文件中以"0"开头的一行后立即停止阅读.我的while循环遇到了不等式运算符的问题.
private void button1_Click(object sender, EventArgs e)
{
// Reading/Inputing column values
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] lines = File.ReadAllLines(ofd.FileName).Skip(8).ToArray();
textBox1.Lines = lines;
while (lines != "0") // PROBLEM Happens Here
{
int[] pos = new int[3] { 0, 6, 18 }; //setlen&pos to read specific colmn vals
int[] len = new int[3] { 6, 12, 28 }; // only doing 3 columns right now
foreach (string line in textBox1.Lines)
{
for (int j = 0; j < 3; j++) // 3 columns
{
val[j] = line.Substring(pos[j], len[j]).Trim(); // each column value in row add to array
list.Add(val[j]); // column values stored in list
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你得到错误,因为lines它string[]不是一个单一的错误string.但是你想要停止在第一行开始"0".所以你可以添加支票foreach:
foreach (string line in lines)
{
if(l.StartsWith("0")) break;
// ...
Run Code Online (Sandbox Code Playgroud)
但是,我会使用此方法来获取相关的行:
var lines = File.ReadLines(ofd.FileName).Skip(8).TakeWhile(l => !l.StartsWith("0"));
Run Code Online (Sandbox Code Playgroud)
区别在于ReadLines不需要处理整个文件.
| 归档时间: |
|
| 查看次数: |
162 次 |
| 最近记录: |