我想在定义的string
中搜索我正在使用foreach
关键字的特定单词,但它不起作用。
我只是个初学者。请帮我解决这个问题,我不想使用数组。
static void Main(string[] args)
{
string str = "Hello You are welcome";
foreach (string item in str) // can we use string here?
{
if (str.Contains(are); // I am checking if the word "are" is present in the above string
Console.WriteLine("True");
)
}
答案 0 :(得分:3)
string str = "Hello You are welcome";
if (str.Contains("are"))
{
Console.WriteLine("True");
}
或者你的意思是:
string str = "Hello You are welcome";
foreach (var word in str.Split()) // split the string (by space)
{
if (word == "are")
{
Console.WriteLine("True");
}
}
答案 1 :(得分:-1)
试试这个
static void Main(string[] args)
{
string str = "Hello You are welcome";
foreach (var item in str.Split(' ')) // split the string (by space)
{
if (item == "are")
{
Console.WriteLine("True");
}
}
}