【发布时间】:2020-11-22 15:52:59
【问题描述】:
我在这里有这段代码,如果我的输入是大写或小写,我正在尝试独立搜索联系人中的姓名(例如,如果我输入“john”/”,则独立搜索联系人“John” John"/"jOhn"/etc) 但我不知道该怎么做。
using System;
namespace Exercise_8._5
{
class Program
{
static void Main(string[] args)
{
string[,] sContacts = new string [5,2];
string sSearch = "";
int iRows = 5;
int iSub = 0;
ReadContacts(ref sContacts, iRows);
Search(ref sSearch, iSub);
Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
static void Contacts(ref string[,] psContacts)
{
psContacts[0, 0] = "John";
psContacts[0, 1] = "07621456900";
psContacts[1, 0] = "Jasper";
psContacts[1, 1] = "07843456377";
psContacts[2, 0] = "Jane";
psContacts[2, 1] = "07935254678";
psContacts[3, 0] = "Jim";
psContacts[3, 1] = "07945112623";
psContacts[4, 0] = "Jackie";
psContacts[4, 1] = "07431733507";
}
static void ReadContacts(ref string[,] psContacts, int piRows)
{
Contacts(ref psContacts);
Console.WriteLine("My top 5 Contacts are: ");
Console.WriteLine();
for (piRows = 0; piRows < psContacts.GetLength(0); piRows++)
{
Console.Write(psContacts[piRows, 0] + " - ");
Console.WriteLine(psContacts[piRows, 1]);
}
}
static void Name(ref string psSearch)
{
Console.WriteLine();
Console.WriteLine("Search: ");
psSearch = Console.ReadLine();
}
static void Search(ref string psSearch, int piSub)
{
int piRows = 5;
int piColoumns = 2;
string[,] psContacts = new string[piRows, piColoumns];
Contacts(ref psContacts);
Name(ref psSearch);
while (piSub < psContacts.GetLength(0) && psSearch != psContacts[piSub, 0])
{
String.Compare(psSearch, psContacts[piSub, 0], true);
piSub++;
}
if (piSub != psContacts.GetLength(0))
{
for(piRows = 0; piRows < 5; piRows++)
{
if (psSearch == psContacts[piRows, 0])
{
Console.WriteLine();
Console.Write(psContacts[piRows, 0] + " - ");
Console.WriteLine(psContacts[piRows, 1]);
}
}
}
else
{
for(piSub = 0; piSub < 1; piSub++)
{
Console.WriteLine();
Console.WriteLine(psSearch + " doesn't exist");
}
}
}
}
}
【问题讨论】:
-
你说你不知道怎么做,但是你有行
String.Compare(psSearch, psContacts[piSub, 0], true);就是这样做的......(我没有阅读整个代码,但 that's what that overload 确实. -
这能回答你的问题吗? Caselessly comparing strings in C#
-
有人告诉我这个 - String.Compare(psSearch, psContacts[piSub, 0], true); - 我决定尝试一下,但它什么也没做,我仍然无法以其他方式搜索。我不知道我是否将它放在一个不好的地方,或者我是否应该将它写在另一个函数中并读取所有联系人和用户的输入并进行比较。我尝试了在互联网上看到的其他不同方法,但没有一个有效。
标签: c# compare string-comparison