Search generic list for a searchword
Heeelp! deadline soon and i'm stuck..
I have a list like this:
List<string[]> blogPostList = new();
that contains this:
string[] blogPost = new string[3];
I have a linear search that lets you search for a title, but this ONLY matches if the "full title" is a match.
I need to make something that can take a searchword, search blogPostList[][0] (which is title) and match the searchword this to ANY part of the string title
any ideas?
15 Replies
You could probably use
.Contains()
?Angius#1586
REPL Result: Success
Result: bool
Compile: 419.448ms | Execution: 34.779ms | React with ❌ to remove this embed.
I have been googling .Contains() for an hour and can't seem to find how to make this work with the list<>
any pointers?
listName[i][0]
is the title, right?yes
Contains is for a single string
listName[i][0].ToLower().Contains(searchTerm.ToLower())
Or better yet,
listName[i][0].Contains(searchTerm, StringComparison.CurrentCultureIgnoreCase)
OMG! it works, now it matches regardless if the full title is matched or just a word in the title
If i want this to keep going and show all matches / "potential" matches to a seachword, how would i do that?
You would have to create an empty list where you'd keep your matches
Then, instead of returning a match in the loop, you'd add it to the list
And, lastly, after the loops are done, you'd return that list
Alternatively, you could use an
IEnumerable
and yield return
, but I don't think there was any mention of that during your lessons yet, seeing how you're using arrays to represent objectsaaaaaaaaah thank youuuu, i'll try this and be back!
yes unfortunately, it was really hard to google this because all answers used more advanced code
I love you, thanks ❤️