Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文件中,如果行包含子字符串,则从右侧获取所有行_C#_Substring - Fatal编程技术网

C# 在文件中,如果行包含子字符串,则从右侧获取所有行

C# 在文件中,如果行包含子字符串,则从右侧获取所有行,c#,substring,C#,Substring,我有一份档案。每一行看起来如下所示: [00000] 0xD176234F81150469: foo 我试图做的是,如果一行包含某个子字符串,我想提取找到的子字符串右侧的所有内容。例如,如果我在上行中搜索0xD176234F81150469:,它将返回foo。每个字符串的长度可变。我用的是C# 值得注意的是,文件中的每一行都与上面的类似,在左边的方括号中包含一个以16为基数的数字,后面是一个十六进制哈希和分号,后面是一个英文字符串 我该怎么做呢 编辑 这是我的密码: private v

我有一份档案。每一行看起来如下所示:

[00000] 0xD176234F81150469: foo
我试图做的是,如果一行包含某个子字符串,我想提取找到的子字符串右侧的所有内容。例如,如果我在上行中搜索
0xD176234F81150469:
,它将返回
foo
。每个字符串的长度可变。我用的是C#

值得注意的是,文件中的每一行都与上面的类似,在左边的方括号中包含一个以16为基数的数字,后面是一个十六进制哈希和分号,后面是一个英文字符串

我该怎么做呢

编辑

这是我的密码:

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 box = new Form1();
        if(MessageBox.Show("This process may take a little while as we loop through all the books.", "Confirm?", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
        {
            XDocument doc = XDocument.Load(@"C:\Users\****\Desktop\books.xml");

            var Titles = doc.Descendants("Title");

            List<string> list = new List<string>();

            foreach(var Title in Titles)
            {
                string searchstr = Title.Parent.Name.ToString();
                string val = Title.Value;
                string has = @"Gameplay/Excel/Books/" + searchstr + @":" + val;
                ulong hash = FNV64.GetHash(has);
                var hash2 = string.Format("0x{0:X}", hash);

                list.Add(val + " (" + hash2 + ")");
                // Sample output: "foo (0xD176234F81150469)"
            }

            string[] books = list.ToArray();

            File.WriteAllLines(@"C:\Users\****\Desktop\books.txt", books);
        }
        else
        {
            MessageBox.Show("Aborted.", "Aborted");
        }
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
Form1框=新Form1();
if(MessageBox.Show(“当我们循环浏览所有书籍时,此过程可能需要一点时间。”,“确认?”,MessageBoxButtons.YesNo,MessageBoxIcon.Information)=DialogResult.Yes)
{
XDocument doc=XDocument.Load(@“C:\Users\***\Desktop\books.xml”);
var Titles=文件子体(“标题”);
列表=新列表();
foreach(标题中的var标题)
{
string searchstr=Title.Parent.Name.ToString();
字符串val=Title.Value;
字符串有=@“Gameplay/Excel/Books/”+searchstr+@:“+val;
ulong hash=FNV64.GetHash(has);
var hash2=string.Format(“0x{0:X}”,散列);
列表。添加(val+”(“+hash2+”);
//示例输出:“foo(0xD176234F81150469)”
}
string[]books=list.ToArray();
writeAllines(@“C:\Users\***\Desktop\books.txt”,books);
}
其他的
{
MessageBox.Show(“已中止。”,“已中止”);
}
}

我还遍历了文件的每一行,将其添加到
列表中。我一定是在尝试这些建议时无意中删除了这个。另外,我对C#很陌生。我遇到的主要问题是匹配问题。

您可以使用
File.ReadLines
和以下Linq查询:

string search = "0xD176234F81150469:";
IEnumerable<String> lines = File.ReadLines(path)
    .Select(l => new { Line = l, Index = l.IndexOf(search) })
    .Where(x => x.Index > -1)
    .Select(x => x.Line.Substring(x.Index + search.Length));

foreach (var line in lines)
    Console.WriteLine("Line: " + line);
string search=“0xD176234F81150469:”;
IEnumerable lines=File.ReadLines(路径)
.Select(l=>new{Line=l,Index=l.IndexOf(search)})
.其中(x=>x.索引>-1)
.Select(x=>x.Line.Substring(x.Index+search.Length));
foreach(行中的var行)
控制台写入线(“行:+行);

如果您不想使用Linq查询,则此选项有效

//"I also iterated through every line of the file, adding it to a list<>." Do this again.
List<string> li = new List<string>()

//However you create this string make sure you include the ":" at the end.
string searchStr = "0xD176234F81150469:"; 

private void button1_Click(object sender, EventArgs e)
{
    foreach (string line in li)
    {
        string[] words;
        words = line.Split(' '); //{"[00000]", "0xD176234F81150469:", "foo"}

        if (temp[1] == searchStr)
        {
            list.Add(temp[2] + " (" + temp[1] + ")");
            // Sample output: "foo (0xD176234F81150469)"
        }
    }
}
/“我还遍历了文件的每一行,将其添加到列表中。”请再次执行此操作。
List li=新列表()
//无论如何创建此字符串,请确保在末尾包含“:”。
字符串searchStr=“0xD176234F81150469:”;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
foreach(以li表示的字符串行)
{
字符串[]个单词;
words=line.Split(“”);//{[00000],“0xD176234F81150469:,“foo”}
if(temp[1]==searchStr)
{
添加(临时[2]+“(“+temp[1]+”));
//示例输出:“foo(0xD176234F81150469)”
}
}
}

不幸的是,其他解决方案都不适合我。我正在使用foreach迭代散列,因此我将不必要地对所有项进行数百万次迭代。最后,我做到了:

            using (StreamReader r = new StreamReader(@"C:\Users\****\Desktop\strings.txt"))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    lines++;
                    if (lines >= 6)
                    {
                        string[] bits = line.Split(':');

                        if(string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        try
                        {
                            strlist.Add(bits[0].Substring(10), bits[1]);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
            }

            foreach(var Title in Titles)
            {
                string searchstr = Title.Parent.Name.ToString();
                string val = Title.Value;
                string has = @"Gameplay/Excel/Books/" + searchstr + ":" + val;
                ulong hash = FNV64.GetHash(has);
                var hash2 = " " + string.Format("0x{0:X}", hash);

                try
                {
                    if (strlist.ContainsKey(hash2))
                    {
                        list.Add(strlist[hash2]);
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    continue;
                }
            }

这使我在短时间内获得了预期的输出。

那么,哪一位给您带来了困难?接受子字符串?寻找匹配的线?从一行一行地迭代文件开始?这通常不是一个很难编程的操作。你能给我们看看你的代码和你被困的地方吗?然后我们将尝试向正确的方向提供建议。我已经在我的原始帖子中添加了更多信息。
            using (StreamReader r = new StreamReader(@"C:\Users\****\Desktop\strings.txt"))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    lines++;
                    if (lines >= 6)
                    {
                        string[] bits = line.Split(':');

                        if(string.IsNullOrWhiteSpace(line))
                        {
                            continue;
                        }

                        try
                        {
                            strlist.Add(bits[0].Substring(10), bits[1]);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
            }

            foreach(var Title in Titles)
            {
                string searchstr = Title.Parent.Name.ToString();
                string val = Title.Value;
                string has = @"Gameplay/Excel/Books/" + searchstr + ":" + val;
                ulong hash = FNV64.GetHash(has);
                var hash2 = " " + string.Format("0x{0:X}", hash);

                try
                {
                    if (strlist.ContainsKey(hash2))
                    {
                        list.Add(strlist[hash2]);
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    continue;
                }
            }