C# 在txt文档中查找标记

C# 在txt文档中查找标记,c#,C#,好吧,我尝试了你们所有人建议的所有东西,但它仍然不适合我。我不知道为什么,但它有时不会踢出输出,有时它会踢出不正确的输出。我做错了什么 InPutBox = Input.Text; int x = 1; var lines = Input.Text.Split(new string[] { Environment.NewLine },StringSplitOptions.None); for (var index = 0; index <

好吧,我尝试了你们所有人建议的所有东西,但它仍然不适合我。我不知道为什么,但它有时不会踢出输出,有时它会踢出不正确的输出。我做错了什么

 InPutBox = Input.Text;
        int x = 1;
        var lines = Input.Text.Split(new string[] { Environment.NewLine },StringSplitOptions.None);

        for (var index = 0; index < lines.Length; index++)
        {

            var line = lines[index];

            do
            {
                x++;
                System.Console.WriteLine("'{0}'", InPutBox);
                bool test1 = InPutBox.StartsWith("11600");
                bool test2 = InPutBox.EndsWith("(");
                if (test1 && test2)
                {
                    int first = InPutBox.IndexOf("11600");
                    int last = InPutBox.LastIndexOf("(");
                    InPutBox = InPutBox.Substring(first, last - first);
                }
            }
            while (x < 50);
            System.Console.WriteLine("'{0}'", line);
            if ((line.StartsWith("11600") && line.EndsWith("(")))
            {
                MessageBox.Show("These are errors in line" + index + ": " + line);
                break;
            }

        }
InPutBox=Input.Text;
int x=1;
var lines=Input.Text.Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
对于(变量索引=0;索引<行.长度;索引++)
{
var行=行[索引];
做
{
x++;
System.Console.WriteLine(“{0}”,InPutBox);
bool test1=InPutBox.StartsWith(“11600”);
bool test2=InPutBox.EndsWith(“”);
if(test1和test2)
{
int first=InPutBox.IndexOf(“11600”);
int last=InPutBox.LastIndexOf(“”);
InPutBox=InPutBox.Substring(第一个,最后一个-第一个);
}
}
而(x<50);
System.Console.WriteLine(“{0}”,第行);
if((第11600行)和第11600行(“(”)和第EndsWith行)
{
Show(“这些是“+索引+”:“+行中的错误);
打破
}
}
这是:

InPutBox = InPutBox.Substring(last, first - last);
可能需要这样:

InPutBox = InPutBox.Substring(first, last - first);
InPutBox = InPutBox.Substring(first, last - first);
可能需要添加或减去1,具体取决于是否要包含
W
0
。在调试器中运行它以查看实际数字,您应该能够更快地进行诊断。

此:

可能需要这样:

InPutBox = InPutBox.Substring(first, last - first);
InPutBox = InPutBox.Substring(first, last - first);

可能需要添加或减去1,具体取决于是否要包含
W
0
。在调试器中运行它以查看实际数字,您应该能够更快地进行诊断。

检查您的条件。对于当前代码,.Substring将在未找到最后一个时抛出错误(因为startIndex不能小于零)

你可能想要这个:

bool test1 = InPutBox.StartsWith("W");
bool test2 = InPutBox.EndsWith("0");
if (test1 && test2) {
    int first = InPutBox.IndexOf("W");
    int last = InPutBox.LastIndexOf("0");
    InPutBox = InPutBox.Substring(last, first - last);
}
@D Stanley可能是对的,因为最后一行也应该颠倒


检查您的条件。对于当前代码,.Substring将在未找到last时抛出错误(因为startIndex不能小于零)

你可能想要这个:

bool test1 = InPutBox.StartsWith("W");
bool test2 = InPutBox.EndsWith("0");
if (test1 && test2) {
    int first = InPutBox.IndexOf("W");
    int last = InPutBox.LastIndexOf("0");
    InPutBox = InPutBox.Substring(last, first - last);
}
@D Stanley可能是对的,因为最后一行也应该颠倒

private void InPutBoxMethod()
{
//将文本拆分为行
var lines=textBox1.Text.Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
//循环所有行并检查错误
对于(变量索引=0;索引<行.长度;索引++)
{
var行=行[索引];
System.Console.WriteLine(“{0}”,第行);
if((第行开始时为(“W”)&第行结束时为(“0”))
{
Show(“这些是“+索引+”:“+行中的错误);
打破
}
}
}
私有void InPutBoxMethod()
{
//将文本拆分为行
var lines=textBox1.Text.Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
//循环所有行并检查错误
对于(变量索引=0;索引<行.长度;索引++)
{
var行=行[索引];
System.Console.WriteLine(“{0}”,第行);
if((第行开始时为(“W”)&第行结束时为(“0”))
{
Show(“这些是“+索引+”:“+行中的错误);
打破
}
}
}