如何从“12345-678”中获取第一个或前两位数字使用c#控制台的子字符串
本文关键字:数字 两位 字符串 控制台 12345-678 第一个 获取 | 更新日期: 2023-09-27 18:15:31
我正在创建一个c#控制台应用程序,该应用程序正在替换零的连字符,以完成我的字符串的最大长度,从身份证"123456-72",我正面临着一个艰难的时刻,必须在我的数组中排序。
我想从这个"123456-72"排序第一个数字或字符,在某些情况下,我需要从前两位数字"391234-56"排序。
这个例子工作得很好,但只针对第一个字符。我需要例子:
class IdentificationNumber {
private string IDNumber;
private int Customertype;
private string check;
private string pad;
private string longFormat;
SortedList GroupID = new SortedList();
public IdentificationNumber(string IDNumber) {
this.IDNumber= IDNumber;
}
public string getLongFormat() {
var ReplaceHyp = IDNumber.Replace("-", "");
int Customertype= Int32.Parse(IDNumber.Substring(0,2));
//Array
//GroupID .Add(1,"Blue");
//GroupID .Add(2,"Blue");
GroupID .Add(38,"White");
GroupID .Add(39,"Blue");
pad="";
check = GroupID.GetByIndex(GroupID.IndexOfKey(Customertype)).ToString();
Console.WriteLine(Customertype);
Console.WriteLine(check);
switch (check) {
case("White"):
longFormat= ReplaceHyp.Substring(0,6)+pad.PadLeft((14 -ReplaceHyp.Length),'0')+ReplaceHyp.Substring(6,(ReplaceHyp.Length-6));
break;
case("Blue"):
longFormat= ReplaceHyp.Substring(0,7)+pad.PadLeft((14 -ReplaceHyp.Length),'0')+ReplaceHyp.Substring(7,(ReplaceHyp.Length-7));
break;
}
return longFormat;
}
}
有什么解决方案或建议吗?
下面是您可能需要的比较器方法的框架:
public static int CompareStrings(string s1, string s2)
{
int Customertype1 = Int32.Parse(s1.Substring(0,2));
int Customertype2 = Int32.Parse(s2.Substring(0,2));
string check1 = GroupID.GetByIndex(GroupID.IndexOfKey(Customertype1)).ToString();
string check2 = GroupID.GetByIndex(GroupID.IndexOfKey(Customertype2)).ToString();
if (Customertype1 > Customertype2)
return 1;
if (Customertype1 < Customertype2)
return -1;
else
{
var ReplaceHyp1 = s1.Replace("-", "");
switch (check1) {
case("White"):
longFormat1 = ReplaceHyp1.Substring(0,6)+pad.PadLeft((14 -ReplaceHyp1.Length),'0')+ReplaceHyp1.Substring(6,(ReplaceHyp1.Length-6));
break;
case("Blue"):
longFormat1 = ReplaceHyp1.Substring(0,7)+pad.PadLeft((14 -ReplaceHyp1.Length),'0')+ReplaceHyp1.Substring(7,(ReplaceHyp1.Length-7));
break;
}
var ReplaceHyp2 = s2.Replace("-", "");
switch (check2) {
case("White"):
longFormat2 = ReplaceHyp2.Substring(0,6)+pad.PadLeft((14 -ReplaceHyp2.Length),'0')+ReplaceHyp2.Substring(6,(ReplaceHyp2.Length-6));
break;
case("Blue"):
longFormat2 = ReplaceHyp2.Substring(0,7)+pad.PadLeft((14 -ReplaceHyp2.Length),'0')+ReplaceHyp2.Substring(7,(ReplaceHyp2.Length-7));
break;
}
return stringCompare(longFormat1, longFormat2);
}
}
这段代码急需重构!根据您的具体需要,我认为可以取消Customertype1/2
的检查。