In the file, if the line contains a substring, get the entire line to the right

I have a file. Each line is as follows:

[00000] 0xD176234F81150469: foo 

I am trying to do this if the string contains a specific substring, I want to extract everything that is to the right of the found substring. For example, if I were looking for 0xD176234F81150469: in the line above, it would return foo . Each line has a variable length. I am using C #.

As a note, each line in the file looks like above, with the base-16 number enclosed in square brackets on the left, followed by a hexadecimal hash and a semicolon, and then an English string.

How can i do this?

Edit

Here is my code:

  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"); } } 

I also repeated every line of the file, adding it to list<> . I must have accidentally deleted this while trying to suggest. Also, I am very new to C #. The main thing I came across is a coincidence.

+4
source share
4 answers

Unfortunately, none of the other solutions worked for me. I repeated hashes using foreach, so I could easily iterate over all objects millions of times. In the end, I did this:

  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; } } 

This gave me the result that I expected in a short period of time.

0
source

You can use File.ReadLines and this Linq query:

 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); 
+4
source

This works if you do not want to use the Linq query.

 //"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)" } } } 
+1
source
 string file = ... string search= ... var result = File.ReadLines(file) .Where(line => line.Contains(search)) .Select(line => line.Substring( line.IndexOf(search) + search.Length + 1); 
0
source

Source: https://habr.com/ru/post/1479498/


All Articles