StartsWith()函数与字符串数组

huangapple go评论66阅读模式
英文:

StartsWith() with an array of strings

问题

让我们假设我有一个字符串数组:

string[] greeting = new string[] {"hey", "hello", "hi"};

如果用户输入是以下任何一个词语:"hey""hello""hi",那么我希望控制台输出 "hey there"。这是我做的:

string userInput = Console.ReadLine();

bool result_a = greeting.Contains(userInput);

if (result_a) { Console.WriteLine("hey there"); }

但似乎不起作用。
英文:

Let's say I have an array of strings:

string[] greeting = new string[] {"hey", "hello", "hi"};

if the user input is any of the following words: "hey" "hello" "hi" then I want the console to print "hey there". This is what I did:

 string userInput = Console.ReadLine();

 bool result_a = userInput.StartsWith(greeting);

 if(result_a) { Console.WriteLine("hey there"); }

but that doesn't seem to be working.

答案1

得分: 1

以下是翻译好的内容:

如果您想要的是,每当用户以以下任何一种方式开始时:

using System;
using System.Linq;

...

string[] greeting = new string[] {"hey", "hello", "hi"};
string userInput = Console.ReadLine();
bool result_a = greeting.Any(a => userInput.Trim().StartsWith(a));
if(result_a) { Console.WriteLine("hey there"); }

示例:“hi 我的名字是 Ali”

现在,如果您希望大写字母也能正常工作,您需要更改以下行:

示例:“Hi 我的名字是 Ali”

bool result_a = greeting.Any(a => userInput.Trim().ToLower().StartsWith(a));

如果您想要的是,每当用户输入包含以下内容时:

using System;
using System.Linq;

...

string[] greeting = new string[] {"hey", "hello", "hi"};
string userInput = Console.ReadLine();
bool result_a = greeting.Any(a => userInput.Trim().Contains(a));
if(result_a) { Console.WriteLine("hey there"); }

示例:“早上好!hello”

现在,如果您希望大写字母也能正常工作,您需要更改以下行:

示例:“早上好!Hello”

bool result_a = greeting.Any(a => userInput.Trim().ToLower().Contains(a));
英文:

If what you want is, whenever the user starts with any of the 3

using System;
using System.Linq;

...

         string[] greeting = new string[] {"hey", "hello", "hi"};
		 string userInput = Console.ReadLine();			
		 bool result_a = greeting.Any(a => userInput.Trim().StartsWith(a));
		 if(result_a) { Console.WriteLine("hey there"); }

Example: "hi my name is Ali"

Now if you want capital letters to work as well you need to change the line to:

Example: "Hi my name is Ali"

bool result_a = greeting.Any(a => userInput.Trim().ToLower().StartsWith(a));

If what you want is, whenever the user input contains then:

using System;
using System.Linq;

...

         string[] greeting = new string[] {"hey", "hello", "hi"};
         string userInput = Console.ReadLine();         
         bool result_a = greeting.Any(a => userInput.Trim().Contains(a));
         if(result_a) { Console.WriteLine("hey there"); }

Example: "Good morning! hello"

Now if you want capital letters to work as well you need to change the line to:

Example: "Good morning! Hello"

bool result_a = greeting.Any(a => userInput.Trim().ToLower().Contains(a));

答案2

得分: 0

string[] greeting = new string[] { "hey", "hello", "hi" };
string userInput = Console.ReadLine();

bool result_a = greeting.Any(x => x.StartsWith(userInput));

if (result_a) { Console.WriteLine("hey there"); }
if (result_a) { Console.WriteLine($"{greeting.FirstOrDefault(x => x.StartsWith(userInput))} there"); }
英文:
string[] greeting = new string[] { "hey", "hello", "hi" };
string userInput = Console.ReadLine();

bool result_a = greeting.Any(x=>x.StartsWith(userInput));

if (result_a) { Console.WriteLine("hey there"); }
if (result_a) { Console.WriteLine($"{greeting.FirstOrDefault(x => x.StartsWith(userInput))} there"); }

答案3

得分: -1

尝试这个,兄弟

List<string> greeting = new List<string>();
greeting.Add("hey");
greeting.Add("hello");
greeting.Add("hi");
string userInput = Console.ReadLine();

if (greeting.Contains(userInput.Trim()))
{
    Console.WriteLine("hey there");
}
英文:

try this,bro

        List&lt;string&gt; greeting = new List&lt;string&gt;(); 
        greeting.Add(&quot;hey&quot;);
        greeting.Add(&quot;hello&quot;);
        greeting.Add(&quot;hi&quot;);
        string userInput = Console.ReadLine();

        if (greeting.Contains(userInput.Trim()))
        {
            Console.WriteLine(&quot;hey there&quot;);
        }

huangapple
  • 本文由 发表于 2023年1月6日 14:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定