C#包含字符串的一部分 [英] c# contains part of string
问题描述
所以我有一个带有Materiel对象的列表。在Materiel中,我有15种get和set方法。我想构造一个搜索方法,使列表中的所有对象以及每个Materiel对象中的所有变量循环。循环部分很容易,但是我在字符串包含部分方面苦苦挣扎。搜索词可以例如是 acto,而我应该会因为 Tractor获得成功。我试过使用string-Contains类,但据我所知,它仅检查从位置0开始的字符串。因此, Tra受到了打击,但没有受到 acto影响。
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?
对不起,不好的解释。
我的代码。现在我看到该子字符串得到了命中,而且还有其他结果:)
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);
}
}
}
推荐答案
某些字符串。包含( str)
将返回true,您是否对大小写敏感?
"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);
这篇关于C#包含字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!