如何比较字符串数组的值?(How do I compare the value of string array? [duplicate])

编程入门 行业动态 更新时间:2024-10-23 14:22:54
如何比较字符串数组的值?(How do I compare the value of string array? [duplicate])

这个问题在这里已有答案:

如何搜索String数组 13 中的字符串 答案 检查字符串数组是否包含值,如果是,则获取其位置 12的答案

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

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") }

结果输出不是我所期望的,任何想法我怎么能去做?

This question already has an answer here:

How to search a string in String array 13 answers Checking if a string array contains a value, and if so, getting its position 12 answers

I have declared a string array and I want to compare with name given by user,

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") }

The resulting output is not what I am expecting, any ideas how I can go about it?

最满意答案

bool found = false; foreach (string s in MasterList) { if(s == YourName) found = true; } if(found) Console.WriteLine("Your name is among the list"); else Console.WriteLine("Your name is not among the list"); bool found = false; foreach (string s in MasterList) { if(s == YourName) found = true; } if(found) Console.WriteLine("Your name is among the list"); else Console.WriteLine("Your name is not among the list");

更多推荐

本文发布于:2023-08-07 05:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1461224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   compare   duplicate   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!