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

标签 c# string visual-studio

作为序言,我从数据库中提取记录。 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/

相关文章:

c# - 下拉列表-确认-问题

c# - 在 Outlook 加载项中以 MIME 格式 (*.eml) 保存邮件

c# - 异步运行同步方法的正确方法是什么

c++ - 将字符串文字传递给采用 std::string& 的函数

c++ - 如何使用 visual c++ 中的 strncpy_s 函数?

c++ - 避免多态类中的虚表

c# - 如何检查一个单词是否以给定字符开头?

C++ 用空格分割一个字符串,除非它被引号括起来并存储在一个 vector 中

c++ - gcc '-rdynamic' 标志等同于 MSVC

c# - 以正确的方式在 C# 中解析字符串