gpt4 book ai didi

c# - 识别字符串并正确操作

转载 作者:行者123 更新时间:2023-11-30 17:38:35 26 4
gpt4 key购买 nike

作为序言,我从数据库中提取记录。 CaseNumber 列将具有唯一标识符。但是,与一个事件相关的多个案例将具有非常相似的案例编号,其中最后两位数字将是下一个编号。示例:

TR42X2330789
TR42X2330790
TR42X2330791
TR51C0613938
TR51C0613939
TR51C0613940
TR51C0613941
TR51C0613942
TR52X4224749

如您所见,我们必须将这些记录分为三组。目前我的功能真的很困惑,我没有考虑到一组案例编号后面跟着另一组案例编号的情况。我想知道是否有人对如何解决这个问题有任何建议。我正在考虑将所有案例编号放在一个数组中。

int i = 1;
string firstCaseNumber = string.Empty;
string previousCaseNumber = string.Empty;

if (i == 1)
{
firstCaseNumber = texasHarrisPublicRecordInfo.CaseNumber;

i++;
}
else if (i == 2)
{
string previousCaseNumberCode = firstCaseNumber.Remove(firstCaseNumber.Length - 3);
int previousCaseNumberTwoCharacters = Int32.Parse(firstCaseNumber.Substring(Math.Max(0, firstCaseNumber.Length - 2)));

string currentCaseNumberCode = texasHarrisPublicRecordInfo.CaseNumber.Remove(texasHarrisPublicRecordInfo.CaseNumber.Length - 3);
int currentCaselastTwoCharacters = Int32.Parse(texasHarrisPublicRecordInfo.CaseNumber.Substring(Math.Max(0, texasHarrisPublicRecordInfo.CaseNumber.Length - 2)));

int numberPlusOne = previousCaseNumberTwoCharacters + 1;

if (previousCaseNumberCode == currentCaseNumberCode && numberPlusOne == currentCaselastTwoCharacters)
{
//Group offense here

i++;

needNewCriminalRecord = false;
}
else
{
//NewGRoup here
}
previousCaseNumber = texasHarrisPublicRecordInfo.CaseNumber;
i++;
}
else
{
string beforeCaseNumberCode = previousCaseNumber.Remove(previousCaseNumber.Length - 3);
int beforeCaselastTwoCharacters = Int32.Parse(previousCaseNumber.Substring(Math.Max(0, previousCaseNumber.Length - 2)));

string currentCaseNumberCode = texasHarrisPublicRecordInfo.CaseNumber.Remove(texasHarrisPublicRecordInfo.CaseNumber.Length - 3);
int currentCaselastTwoCharacters = Int32.Parse(texasHarrisPublicRecordInfo.CaseNumber.Substring(Math.Max(0, texasHarrisPublicRecordInfo.CaseNumber.Length - 2)));

int numberPlusOne = beforeCaselastTwoCharacters + 1;

if (beforeCaseNumberCode == currentCaseNumberCode && numberPlusOne == currentCaselastTwoCharacters)
{
i++;

needNewCriminalRecord = false;
}
else
{
needNewCriminalRecord = true;
}
}

最佳答案

如果您不太关心性能,您可以使用 LINQ .GroupBy().ToDictionary() 方法并创建包含列表的字典。行中的一些东西:

string[] values = 
{
"TR42X2330789",
"TR42X2330790",
"TR42X2330791",
"TR51C0613938",
"TR51C0613939",
"TR51C0613940",
"TR51C0613941",
"TR51C0613942",
"TR52X4224749"
};

Dictionary<string, List<string>> grouppedValues = values.GroupBy(v =>
new string(v.Take(9).ToArray()), // key - first 9 chars
v => v) // value
.ToDictionary(g => g.Key, g => g.ToList());

foreach (var item in grouppedValues)
{
Console.WriteLine(item.Key + " " + item.Value.Count);
}

输出:

TR42X2330   3
TR51C0613 5
TR52X4224 1

关于c# - 识别字符串并正确操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36552851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com