c# - 如何比较字符串数组的值?

标签 c# asp.net

<分区>

我已经声明了一个字符串数组,我想与用户给出的名称进行比较,

string[] MasterList = new string[] {
  "Askay", "Puram", "Raman", "Srinivasa",
  "Gopal", "Rajesh", "Anju", "Nagara",
};

string YourName;
Console.WriteLine("Enter your name: ");
YourName = Console.ReadLine();

for(i=0; i<5; i++)
{
    a = String.Compare(MasterList[i], YourName);
    Console.WriteLine("Your name is not among the list")
}

结果输出不是我所期望的,有什么想法可以解决吗?

最佳答案

为什么不包含

  string[] MasterList = new string[] {
    "Askay", "Puram", "Raman", "Srinivasa",
    "Gopal", "Rajesh", "Anju", "Nagara",
  };

  Console.WriteLine("Enter your name: ");
  string YourName = Console.ReadLine();

  // StringComparer.OrdinalIgnoreCase if you want to ignore case
  // MasterList.Contains(YourName) if you want case sensitive 
  if (!MasterList.Contains(YourName, StringComparer.OrdinalIgnoreCase))
    Console.WriteLine("Your name is not among the list")

关于c# - 如何比较字符串数组的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44951633/

相关文章:

asp.net - 在新计算机上编译ASP.NET网站时无法生成代码错误

asp.net - Razor 不会自动为 View 创建共享文件夹?为什么不?

javascript - 在元素上设置验证样式

c# - 子实体如何从一个父实体移动到另一个父实体?

C# 数组和属性

c# - WP7 中的加速度计问题

c# - 检测损坏的信用卡跟踪数据

c# - 如何获取 UserName/My Pictures 文件夹的目标路径?

c# - 多对多 EF Core 关系 - 为什么它尝试将实体插入到不同的表中?

c# - 为什么不能 UnRegisterStartupScript?