C
C#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?
A
Angius489d ago
You could probably use .Contains()?
M
MODiX489d 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.
R
Ronnie489d ago
I have been googling .Contains() for an hour and can't seem to find how to make this work with the list<>
R
Ronnie489d ago
R
Ronnie489d ago
any pointers?
A
Angius489d ago
listName[i][0] is the title, right?
R
Ronnie489d ago
yes
T
TheBoxyBear489d ago
Contains is for a single string
A
Angius489d ago
listName[i][0].ToLower().Contains(searchTerm.ToLower()) Or better yet, listName[i][0].Contains(searchTerm, StringComparison.CurrentCultureIgnoreCase)
R
Ronnie489d ago
OMG! it works, now it matches regardless if the full title is matched or just a word in the title
R
Ronnie489d ago
R
Ronnie489d ago
If i want this to keep going and show all matches / "potential" matches to a seachword, how would i do that?
A
Angius489d 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
R
Ronnie489d 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
R
Ronnie489d 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 Azu❔ 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❔ Is there any tool converting csv file to Class (seeding data)?I am using EF to seed data and I have an excel file containing like 500 records. If that's around 1❔ Is it possible to have WPF window hidden until Blazor WebView loads page?I'm trying to hide a WPF window until the Blazor WebView loads, but it seems that the initialization❔ SQLite in-memory Database read from ReadOnlyMemory bytesI'm receiving an SQLite database payload from an AMQP-0-9-1 message in the form of `ReadOnlyMemory<bSettings.Default not savingI have two integers, primary and secondary, that i want to persist, but they refuse to❔ need help opening siteshow do i make visual studio open a site and then press on a bar to type something❔ Generally, how do you implement a readonly struct?Hi. Generally, how do you implement a readonly struct? I mean define readonly fields or getter? What❔ How can I query a JSON doc for only objects with a specific property value?I have a JSON object that looks like this: ```json { "pagedata": [ { "id": "❔ Are TryAsync methods acceptable?I've just been refactoring some implementations of a method I have to go from `T Load()` to `bool Tr❔ Wpf chess board problemHello! Im trying to color a chess board. I have a bunch of border tags for the squares. I think it c❔ Errors when everything looks ok.So, I'm dumb a aspnet api, with controllers, using sqlite. There are no syntax errors in the code, s✅ How do I merge a list of dictionaries based on keys using LINQ?So if I have something like: `[{ number: 1 }, { number: 2 }, { number: 3 }]` I want to get: `{ numbe