Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I search for a specific string from My.Resources.filetxt and return whole line?

VB
e.g My.Resources.filetxt has following entries say :


england is cold country
england is cold country
england is cold country


now i want to search this txt file for a string "england" and return the entire line containing this string. How can i do it

What I have tried:

Findstring = My.Resources.filetxt
       Lookfor = TextBox2.Text
       'TextBox1.Text = IO.File.ReadAllLines(My.Resources.filetxt).FirstOrDefault(Function(x) x.Contains(Lookfor))
Posted
Updated 24-Jan-18 21:41pm

1 solution

Two ways to do it:
1) Split the text into separate lines, and search each line for your text. This isn't particularly efficient, particularly if your text is long and contains many lines as it generates a lot of separate strings. It is easy to implement though - a couple of lines of code.
2) Use IndexOf to find each occurance of the string in turn: String.IndexOf Method (String, Int32) (System)[^]. You can then use it again with the returned index to find the end of the line, and String.LastIndexOf Method (String, Int32) (System)[^] to find the beginning. You can then extract the whole line using String.Substring Method (Int32, Int32) (System)[^]. This is a little more work, but will be a lot quicker, and a whole load less of a memory hog!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900