Search String- Search text file using a textbox
Search String- Search text file using a textbox
I presently have a multi line textBox that has acquired a list from another source.This list looks like this:
48:5D:36:4E:63:F8
00:24:1D:23:97:CF
4C:CC:6A:F4:42:2C
My Goal is:
I am trying to use the line items in this textBox list to find a match in a text file. What is important is when it does not find a line match in the text file, a messagebox appears saying "Item is not on list'...and include the actual textBox line item that wasn't found in the text file.
I am presently trying to work the following:
string find = textBox16.Text;
foreach (string line in File.ReadAllLines("watcher.txt"))
{
if (line.Contains(find))
{
//Do nothing
}
else
{
MessageBox.Show("Item not on list");
}
}
I've also tried this:
foreach (string line in textBox16.Lines)
{
if (File.ReadAllLines("watcher.txt").Contains(textBox16.Text))
{
//do nothing
}
else
{
MessageBox.Show("Item not on list");
}
}
I've tried several other ways, but nothing I have tried at this point is working correctly. Any help would be appreciated. Thanks~
2 Answers
2
Something like this i guess
var items = textBox16.Text
.Split(new { "n", "rn" }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
var lines = File.ReadAllLines("watcher.txt");
var missing = items.Where(item => !lines.Contains(item))
.ToList();
if(missing.Any())
MessageBox.Show($"Missing : rn {string.Join("rn", missing)}");
Note : There are more efficient ways to do this with larger files
Important comment from Dai
This assumes you want exact matches (as List.Contains(String)
will call Object.Equals which will perform string ordinal-equality
which may be undesirable, and won't match substrings.
I.e if your text file contains
48:5D:36:4E:63:F8
00:24:1D:23:97:CF
4C:CC:6A:F4:42:2C
...
and Case isn't a problem, then it should work
This assumes you want exact matches (as
List<String>.Contains(String)
will call Object.Equals
which will perform string ordinal-equality which may be undesirable, and won't match substrings.– Dai
Jun 30 at 1:15
List<String>.Contains(String)
Object.Equals
@Dai definitely understood and assumed, ill make a note for clarity
– TheGeneral
Jun 30 at 1:16
One last question General if you can please..(a little history) The textbox list being acquired from a listview results list from a network scan that is set on a timer. When I clear the textbox for a new scan, the code will not find the missing items. But if I do not clear the textbox and and let the scan results stack, it works perfectly- any ideas on what may be happening?
– Rick
Jun 30 at 2:30
@Rick If its a threaded timer you will need to
lock
access to your textbox for thread safety.– TheGeneral
Jun 30 at 2:31
lock
Sorry had to edit question.
– Rick
Jun 30 at 2:34
The problem is the textbox's text is not a single value: you're looking for multiple values on every line if the input text file.
You also don't need to read the entire file into memory (which is what ReadAllLines
does), you can read it line-by-line from a StreamReader
instead, this is how you can process multi-gigabyte sized text files while using minimal memory:
ReadAllLines
StreamReader
String findValues = this.textBox16.Text.Split( new String { "rn" }, StringSplitOptions.RemoveEmptyEntries );
List<Int32> matchingLineNumbers = new List<Int32>();
using( StreamReader rdr = new StreamReader( "watcher.text" ) )
{
String line;
Int32 lineNumber = 1;
while( ( line = rdr.ReadLine() ) != null )
{
foreach( String value in findValues )
{
if( line.IndexOf( value, StringComparison.OrdinalIgnoreCase ) > -1 )
{
matchingLineNumbers.Add( lineNumber );
}
}
lineNumber++;
}
}
if( matchingLineNumbers.Count == 0 )
{
MessageBox.Show( "Item not on list" );
}
else
{
String message = "Matching lines: " + String.Join( ", ", matchingLineNumbers.Select( n => n.ToString() ) );
MessageBox.Show( message );
}
Thank you! I've been trying breakdown and research to understand the responses. Both are so different but both are starting to make sense. Both responses, (Dai & TheGeneral) work well for my scenario. I am going need to research and absorb the 2 different approaches to better understand why/if one is more effective than the other. Thank you very much folks for sending me down the right path.
– Rick
Jun 30 at 2:02
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Have you tried Trim().ToLower() or StringComparer.OrdinalIgnoreCase?
– ffhighwind
Jun 30 at 1:10