如何比较字符串数组的值?(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 answersI 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");更多推荐
发布评论