C
C#2y ago
Ronnie

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
Angius
Angius2y ago
You could probably use .Contains()?
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
"hello beautiful world".Contains("beautiful")
"hello beautiful world".Contains("beautiful")
Result: bool
True
True
Compile: 419.448ms | Execution: 34.779ms | React with ❌ to remove this embed.
Ronnie
RonnieOP2y ago
I have been googling .Contains() for an hour and can't seem to find how to make this work with the list<>
Ronnie
RonnieOP2y ago
Ronnie
RonnieOP2y ago
any pointers?
Angius
Angius2y ago
listName[i][0] is the title, right?
Ronnie
RonnieOP2y ago
yes
TheBoxyBear
TheBoxyBear2y ago
Contains is for a single string
Angius
Angius2y ago
listName[i][0].ToLower().Contains(searchTerm.ToLower()) Or better yet, listName[i][0].Contains(searchTerm, StringComparison.CurrentCultureIgnoreCase)
Ronnie
RonnieOP2y ago
OMG! it works, now it matches regardless if the full title is matched or just a word in the title
Ronnie
RonnieOP2y ago
Ronnie
RonnieOP2y ago
If i want this to keep going and show all matches / "potential" matches to a seachword, how would i do that?
Angius
Angius2y ago
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 objects
Ronnie
RonnieOP2y ago
aaaaaaaaah 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
Ronnie
RonnieOP2y ago
I love you, thanks ❤️
Want results from more Discord servers?
Add your server