识别字符串并正确操作

时间:2016-04-11 15:23:08

标签: c# string visual-studio

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

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;
    }
}

2 个答案:

答案 0 :(得分:1)

如果您不关心性能,可以使用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

答案 1 :(得分:0)

我会创建一个普通的puropose扩展方法:

static IEnumerable<IEnumerable<T>> GroupByConsecutiveKey<T, TKey>(this IEnumerable<T> list, Func<T, TKey> keySelector, Func<TKey, TKey, bool> areConsecutive)
{
    using (var enumerator = list.GetEnumerator())
    {
        TKey previousKey = default(TKey);
        var currentGroup = new List<T>();

        while (enumerator.MoveNext())
        {
            if (!areConsecutive(previousKey, keySelector(enumerator.Current)))
            {
                if (currentGroup.Count > 0)
                {
                    yield return currentGroup;
                    currentGroup = new List<T>();
                }
            }

            currentGroup.Add(enumerator.Current);
            previousKey = keySelector(enumerator.Current);
        }

        if (currentGroup.Count != 0)
        {
            yield return currentGroup;
        }
    }
}

现在你可以使用它:

var grouped = data.GroupByConsecutiveKey(item => item, (k1, k2) => areConsecutive(k1, k2));

areConsecutive的快速破解可能是:

public static bool Consecutive(string s1, string s2)
{
    if (s1 == null || s2 == null)
        return false;

    if (s1.Substring(0, s1.Length - 2) != s2.Substring(0, s2.Length - 2))
        return false;

    var end1 = s1.Substring(s1.Length - 2, 2);
    var end2 = s2.Substring(s2.Length - 2, 2);

    if (end1[1]!='0' && end2[1]!='0')
        return Math.Abs((int)end1[1] - (int)end2[1]) == 1;

    return Math.Abs(int.Parse(end1) - int.Parse(end2)) == 1;
}

请注意,我认为Key可以采取任何形式。如果字母数字代码总是具有相同的模式,那么你可以使这个方法更漂亮或只使用正则表达式。