4

我有一个文件。每行如下所示:

[00000] 0xD176234F81150469: foo

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

需要注意的是,文件中的每一行都与上面类似,左侧方括号中包含一个 base-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");
        }
    }

我还遍历了文件的每一行,将其添加到list<>. 我一定是在尝试建议时不小心删除了这个。另外,我对 C# 很陌生。我被难倒的主要事情是匹配。

4

4 回答 4

4

您可以使用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);
于 2013-05-07T09:08:42.777 回答
1

如果您不想使用 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)"
        }
    }
}
于 2013-05-07T09:27:27.370 回答
0
string file = ...
string search= ...
var result = File.ReadLines(file)
              .Where(line => line.Contains(search))
              .Select(line => line.Substring(
                                   line.IndexOf(search) + search.Length + 1);
于 2013-05-07T09:10:49.010 回答
0

不幸的是,其他解决方案都不适合我。我正在使用 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;
                }
            }

这给了我在短时间内预期的输出。

于 2013-05-11T01:43:06.447 回答