在文件中,如果line包含子字符串,则从右侧获取该行的所有内容
本文关键字:获取 如果 文件 line 包含 字符串 | 更新日期: 2023-09-27 18:12:29
我有一个文件。每一行如下所示:
[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");
}
}
我还遍历了文件的每一行,将其添加到list<>
中。我一定是在尝试建议时不小心删除了这个。另外,我对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);
如果您不想使用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)"
}
}
}
string file = ...
string search= ...
var result = File.ReadLines(file)
.Where(line => line.Contains(search))
.Select(line => line.Substring(
line.IndexOf(search) + search.Length + 1);
不幸的是,其他解决方案都不适合我。我使用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;
}
}
这给了我在短时间内所期望的输出。