如何搜索以某个值开头的字符串?

时间:2017-08-03 17:37:17

标签: c# arrays string search wildcard

我正在搜索数组并尝试查找对象描述。我遇到的问题是尝试使用通配符。如何在数组中搜索以“Description:”开头的值。

int[] poss = textlist.Select((b, i) => b == "Description:*" ? i : -1).Where(i => i != -1).ToArray();

string[] Description = new string[poss.Length - 1];

foreach (int pos in poss)
{
    Description = textlist[pos];
}

2 个答案:

答案 0 :(得分:4)

你可以这样做:

Description = textlist.Where(s => s.StartsWith("Description:")).ToArray();

答案 1 :(得分:2)

int[] poss = textlist.Select((b, i) => 
    b.StartsWith("Description") ? i : -1).Where(i => i != -1).ToArray();