检查数组列表是否包含以“”Windows Phone 7 C#开头的数据

时间:2011-08-05 08:04:09

标签: c# windows-phone-7

我正在尝试检查数组列表是否包含以“apple”开头的字符串。

是否有可能显示以“apple”开头的数据?

foreach (var reminder1 in reminderSplit)
{
    MessageBox.Show(reminder1);
    if (reminder1.StartsWith("apple"))
    {
        string home = reminder1.StartsWith("apple").ToString();
        MessageBox.Show("Have : " + home);
    }
}

2 个答案:

答案 0 :(得分:2)

是的,当然:

foreach (var reminder1 in reminderSplit)
{
    MessageBox.Show(reminder1);
    if (reminder1.StartsWith("apple"))
    {
        MessageBox.Show("Have : " + reminder1);
    }
}

或者,如果您要排除“apple”,则可以使用以下内容替换显示代码:

MessageBox.Show("Have : " + reminder1.Substring("apple".Length));

答案 1 :(得分:0)

您可以使用LINQ轻松完成此操作:

var apples = from s in reminderSplit
             where s.StartsWith("apple", StringComparison.OrdinalIgnoreCase)
             select s;

或者,如果您更喜欢某些事情:

var apples = reminderSplit
    .Where(s => s.StartsWith("apple", StringComparison.OrdinalIgnoreCase);

请注意,您可以指定字符串比较的类型 - 此特定比较将执行不区分文化和不区分大小写的匹配,它将同等地匹配“Applesauce”和“applesauce”。