I am trying to check if a array list contains a string that starts with "apple".
Is it possible that if it ex开发者_StackOverflow中文版ist will show the data that start with "apple"?
foreach (var reminder1 in reminderSplit)
{
MessageBox.Show(reminder1);
if (reminder1.StartsWith("apple"))
{
string home = reminder1.StartsWith("apple").ToString();
MessageBox.Show("Have : " + home);
}
}
Yes, of course:
foreach (var reminder1 in reminderSplit)
{
MessageBox.Show(reminder1);
if (reminder1.StartsWith("apple"))
{
MessageBox.Show("Have : " + reminder1);
}
}
Alternatively, if you wish to exclude "apple", then you can replace the display code with the following:
MessageBox.Show("Have : " + reminder1.Substring("apple".Length));
You could do this easily with LINQ:
var apples = from s in reminderSplit
where s.StartsWith("apple", StringComparison.OrdinalIgnoreCase)
select s;
or, if you prefer something terser:
var apples = reminderSplit
.Where(s => s.StartsWith("apple", StringComparison.OrdinalIgnoreCase);
Note that you can specify a type of string comparison - this particular comparison will perform a culture- and case-insensitive match, which will match "Applesauce" and "applesauce" equally.
精彩评论