嗨,我正在尝试验证我的字符串,以便不允许任何以“911”开头的输入。因此,如果您键入:“9 11”,“91 1”,“9 1 1”,它应该通过我的if语句。 它可以使用“911”,但不适用于其他情况。以下是我的代码:
using System;
using System.Collections.Generic;
namespace Phone_List
{
class Program
{
static void Main(string[] args)
{
var phoneList = new List<string>();
string input;
Console.WriteLine("Input: ");
while ((input = Console.ReadLine()) != "")
{
phoneList.Add(input);
for (int i = 0; i < phoneList.Count; i++)
{
if (phoneList[i].Substring(0, 3) == "911")
{
input.StartsWith("9 11");
input.StartsWith("9 1 1");
input.StartsWith("91 1");
Console.WriteLine("NO");
Console.ReadLine();
return;
}
else
{
Console.WriteLine("YES");
Console.ReadLine();
return;
}
}
}
}
}
}
您可以看到,我正在尝试使用"input.StartsWith("9 11")
",但它不起作用...
StartsWith
并丢弃结果。) - Raymond Chen