如何拆分字符串以检查 C# 控制台程序中的所有约束
how to split the string to check all the constraints in C# console program
我想在 C# 中创建一个程序,用户将在其中输入一个字符串,例如
BC12054321 作为方法中的参数。
这里
BC(左起前两个字)会显示它的Bachelor,它可以用MC来代表MCS。
12(左起第3、4个字符代表入学年份不能大于2015小于2012)
04(左起第 5 个和第 6 个字符)代表注册学期。现在可以是 02 或 04 02 代表 Spring 学期,04 代表秋季学期
54321(最后5个字符)代表学生唯一ID。
这就是整个案例,现在我很困惑我使用数组来存储该字符串或简单变量。如果我使用简单变量,那么我如何在单个变量中应用这些所有约束我的意思是我如何拆分这个字符串以检查所有条件。有谁能够帮助我?我还附上了我真正想做的事情,请看 gif 图片:
我会创建一个 class 来表示您正在检索的任何数据。您的数据代表某种东西,因此最好定义它而不是使用值数组。
public class Enrollment
{
public int EnrollmentYear {get;set;}
public int Semester {get;set;}
//etc
}
然后编写一个扩展 class 来解析一个字符串和 returns 一个注册,也许是这样的:
public static class EnrollmentExtensions
{
public Enrollment ParseEnrollment(this string enrollmentString)
{
var enrollment = new Enrollment();
//Now you can inspect substrings and set the properties
//on the class before returning it.
//If the string can't be parsed into an enrollment then
//you could throw an exception.
}
}
这样,将字符串转换为 Enrollment
的所有逻辑都与您的主要方法分开。
你可以这样做:
var input = Console.Readline();
Enrollment enrollment = null;
try
{
enrollment = input.ParseEnrollment();
}
catch(Exception ex)//Whatever exception type you throw
{
//Error message or repeat showing the correct format.
}
将用户输入存储到一个变量中,然后使用 Substring
将每个值拆分为单独的变量。
string studentInfo = Console.ReadLine();
string studentProgram = studentInfo.Substring(0, 2);
string studentYearOfEnrollment = studentInfo.Substring(2, 2);
string studentSemester = studentInfo.Substring(4, 2);
string studentID = studentInfo.Substring(6, 5);
我只会使用 1 个变量
为了验证,您可以使用像这样的简单正则表达式
^(BA|MA)(12|13|14|15)(02|04)\d{5}$
我想在 C# 中创建一个程序,用户将在其中输入一个字符串,例如 BC12054321 作为方法中的参数。 这里 BC(左起前两个字)会显示它的Bachelor,它可以用MC来代表MCS。 12(左起第3、4个字符代表入学年份不能大于2015小于2012) 04(左起第 5 个和第 6 个字符)代表注册学期。现在可以是 02 或 04 02 代表 Spring 学期,04 代表秋季学期 54321(最后5个字符)代表学生唯一ID。
这就是整个案例,现在我很困惑我使用数组来存储该字符串或简单变量。如果我使用简单变量,那么我如何在单个变量中应用这些所有约束我的意思是我如何拆分这个字符串以检查所有条件。有谁能够帮助我?我还附上了我真正想做的事情,请看 gif 图片:
我会创建一个 class 来表示您正在检索的任何数据。您的数据代表某种东西,因此最好定义它而不是使用值数组。
public class Enrollment
{
public int EnrollmentYear {get;set;}
public int Semester {get;set;}
//etc
}
然后编写一个扩展 class 来解析一个字符串和 returns 一个注册,也许是这样的:
public static class EnrollmentExtensions
{
public Enrollment ParseEnrollment(this string enrollmentString)
{
var enrollment = new Enrollment();
//Now you can inspect substrings and set the properties
//on the class before returning it.
//If the string can't be parsed into an enrollment then
//you could throw an exception.
}
}
这样,将字符串转换为 Enrollment
的所有逻辑都与您的主要方法分开。
你可以这样做:
var input = Console.Readline();
Enrollment enrollment = null;
try
{
enrollment = input.ParseEnrollment();
}
catch(Exception ex)//Whatever exception type you throw
{
//Error message or repeat showing the correct format.
}
将用户输入存储到一个变量中,然后使用 Substring
将每个值拆分为单独的变量。
string studentInfo = Console.ReadLine();
string studentProgram = studentInfo.Substring(0, 2);
string studentYearOfEnrollment = studentInfo.Substring(2, 2);
string studentSemester = studentInfo.Substring(4, 2);
string studentID = studentInfo.Substring(6, 5);
我只会使用 1 个变量
为了验证,您可以使用像这样的简单正则表达式
^(BA|MA)(12|13|14|15)(02|04)\d{5}$