So I have a list with Materiel-objects. In Materiel I have 15 get and set methods. I want to construct a search-method that loops all the objects in the list, and all of the variables in each Materiel-object. The looping part is easy enough, but I'm struggling with the string-contains-part. The search term could for instance be "acto", and I should get a hit for "Tractor". I have tried using the string-Contains class, but as far as I can figure out, it only checks the string beginning in position 0. So "Tra" gets a hit, but not "acto".
Is there any build in classes, or should I program one myself?
Sorry for the bad explanation.
My code. I see now that I get hits for the substring, but also other results :)
protected void Button_search_Click(object sender, EventArgs e)
{
string searchTerm = TextBox1.Text.ToString().ToLower();
TableRow row;
TableCell cell;
int rowNumber = 1;
foreach (Materiell mat in allItems)
{
if (searchTerm.Contains(mat.itemID.ToString().ToLower()) ||
searchTerm.Contains(mat.manufacturer.ToLower()) ||
searchTerm.Contains(mat.model.ToLower()) ||
searchTerm.Contains(mat.serialNo.ToLower()) ||
searchTerm.Contains(mat.dateProd.ToString().ToLower()) ||
searchTerm.Contains(mat.location.ToLower()) ||
searchTerm.Contains(mat.mainCategory.ToLower()) ||
searchTerm.Contains(mat.subCategory.ToLower()) ||
searchTerm.Contains(mat.dateAcquired.ToString().ToLower()) ||
searchTerm.Contains(mat.price.ToString().ToLower()) ||
searchTerm.Contains(mat.ownerID.ToString().ToLower()) ||
searchTerm.Contains(mat.extra.ToString().ToLower()) ||
searchTerm.Contains(mat.textComment.ToLower()) ||
searchTerm.Contains(mat.active.ToString().ToLower()))
{
row = new TableRow();
row.ID = "row" + rowNumber.ToString();
rowNumber++;
cell = new TableCell();
cell.Text = "<a href=\"#\" class=\"opendiv\">" + mat.itemID.ToString() + "</a>";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.manufacturer.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.model.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.serialNo.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.dateProd.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.location.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.mainCategory.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.subCategory.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.dateAcquired.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.price.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.ownerID.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.extra.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.ownDefData.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.textComment.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.active.ToString();
row.Cells.Add(cell);
Table1.Rows.Add(row);
}
}
}
"some string".Contains("str")
will return true, are you having problems with case sesitivity?
If so you could use this:
public static bool Contains(this string source, string toCheck, StringComparison comp) {
return source.IndexOf(toCheck, comp) >= 0;
}
string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
(Taken from Case insensitive 'Contains(string)')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With