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
Ronnie2y ago
I have been googling .Contains() for an hour and can't seem to find how to make this work with the list<>
Ronnie
Ronnie2y ago
Ronnie
Ronnie2y ago
any pointers?
Angius
Angius2y ago
listName[i][0] is the title, right?
Ronnie
Ronnie2y 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
Ronnie2y ago
OMG! it works, now it matches regardless if the full title is matched or just a word in the title
Ronnie
Ronnie2y ago
Ronnie
Ronnie2y 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
Ronnie2y 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
Ronnie2y ago
I love you, thanks ❤️
Want results from more Discord servers?
Add your server
More Posts
Remove or overwrite single element of list of arrays(not completely sure its called list of arrays) Basically i have one of these: List<string[]> blog✅ When calling a generic function, how can I tell it that I'm going to be passing null?I've got a generic function which is something like this: ```cs public ReturnType GetValue<ReturnTyp❔ WPF Textbox decimal onlyHello, i'd like to know how we are supposed to configure our textbox in order to accept only decimalmaui deserialize problemI am having a problem when trying to deserialize api data ```C# string content = await response.C❔ Log from helper class without creating new context? (Serilog)I have a bunch of services with their own dedicated loggers that have config'd settings for filterin❔ Swagger Open API not showing Request examples in the UII've written an Azure Function with Open API in C#, but when I open up the Swagger/UI to test my AzuLooking solution for get query from _dbContext.SaveChangesAsync() for save logs and easier debuggingHello, i have a .net core 6.0 app that save entites in MSSQL database with EfCORE 6.0 so my function❔ I'm building an MVC core webshop. How should I be structuring controllers, actions and views?So, right now, I have one single view, and this view renders both information about the product and ❔ How to make an object follows my mouse?so I am new at programming and trying to make a top down shooting games in windows form app. My lect❔ Is this the right way to implement an Interface```csharp interface IObstacles { void Draw(Graphics g); } class Redbox : IO