[英]Using C# to check if string contains a string in string array and how to connect it
基本上我希望用户以字符串形式输入月份,这是我的代码...
static void getAbbreviatedMonth() //dd/mmm/yyyy
{
do
{
Console.Write("PLease enter the year (not earlier than 1812) as 4 digits >> ");
} while (!int.TryParse(Console.ReadLine(), out y) || y < 1812);
do
{
Console.Write("Please enter the month as a three letter `character ( e.g 'Jul'>> ");`
这是我下面的单独编码,我将如何在最高位上编码该编码,以便它从下面检查并匹配字符串。
static bool isCorrectMonth(string monthToCheck)
{
string stringToCheck = monthToCheck;
string[] stringArray = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
foreach (string x in stringArray)
{
if (x.Contains(stringToCheck))
{
// Process..
return true;
}
else
{
return false;
}
}
return false;
}
根据您的示例代码,我认为应该是这样,但是您至少应该通过对所有小写字母进行验证来使其不区分大小写:
do
{
Console.Write("PLease enter the year (not earlier than 1812) as 4 digits >> ");
} while (!int.TryParse(Console.ReadLine(), out y) || y < 1812);
do
{
Console.Write("Please enter the month as a three letter character ( e.g 'Jul'>> ");
} while (!isCorrectMonth(Console.ReadLine()) );
static bool isCorrectMonth(string monthToCheck)
{
string stringToCheck = monthToCheck.ToLower();
string[] stringArray = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
foreach (string x in stringArray)
{
if (x.Contains(stringToCheck))
{
// Process..
return true;
}
else
{
return false;
}
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.