我们如何映射区分大小写的字符串?

时间:2015-03-10 07:49:44

标签: c# model-view-controller

我有我的代码,

string firstLineOfRecord = "front images,Currency Code,Date,BackImages,Domination";
string[] fieldArrayRecord = firstLineOfRecord.Split(',');

string fields = "FrontImages,BackImages,Domination,CurrencyCode,SerialNumber";
string[] fieldArrayList = fields.Split(',');
List<int> mappedList = new List<int>();

for (int i = 0; i< fieldArrayList.Count(); i++)
{
    for (int j = 0; j < fieldArrayRecord.Count(); j++)
    {
        if (fieldArrayList[i] == fieldArrayRecord[j])
        {
            mappedList.Add(j);
        }
    }
}

我如何使用“FrontImages”映射“正面图像”。

作为初学者,我不知道如何解决这个问题。请告诉我如何实现这一目标。

4 个答案:

答案 0 :(得分:2)

对于这种模糊匹配,首先需要识别要忽略的有效标识符(在本例中为空格)。

您可以执行以下操作:删除所有这些标识符。然后比较案例和文化不敏感。

string normalizedHeaderString = "FrontImages";
string normalizedInputString = "front images";

foreach (string c in new[] { " " }) /* the strings to strip out */
{
    normalizedHeaderString = normalizedHeaderString.Replace(c, null);
    normalizedInputString = normalizedInputString.Replace(c, null);
}

if (string.Equals( normalizedHeaderString
                 , normalizedInputString
                 , StringComparison.OrdinalIgnoreCase
                 )
   )
{ /* do your logic, like saving the index, etc */ }

这有点hacky,但你明白了。您最好使用StringComparer的自定义实现,忽略要删除的字符。

答案 1 :(得分:1)

据我所知,你的问题是空格和区分大小写 所以你可以使用

fieldArrayList[i].Replace(" ","").ToLower() == 
    fieldArrayRecord[j].Replace(" ","").ToLower()

答案 2 :(得分:1)

class Program
{
    static void Main(string[] args)
    {
        string firstLineOfRecord = "front images,Currency Code,Date,BackImages,Domination";
        string[] fieldArrayRecord = firstLineOfRecord.Split(',');

        string fields = "FrontImages,BackImages,Domination,CurrencyCode,SerialNumber";
        string[] fieldArrayList = fields.Split(',');
        List<int> mappedList = new List<int>();

        for (int i = 0; i < fieldArrayRecord.Length; i++)
        {
            if (fieldArrayList.Any(s => string.Equals( fieldArrayRecord[i].Replace(" ", string.Empty), s, StringComparison.OrdinalIgnoreCase)))
            {
                mappedList.Add(i);
            }
        }

        foreach (int index in mappedList)
        {
            Console.WriteLine(index);
        }
    }
}

输出:

0
1
3
4

或使用字典:

class Program
{
    static void Main(string[] args)
    {
        string firstLineOfRecord = "front images,Currency Code,Date,BackImages,Domination";

        string fields = "FrontImages,BackImages,Domination,CurrencyCode,SerialNumber";

        var dataFields = firstLineOfRecord.Split(',').Select((x, index) => new { FieldName = x.Replace(" ", string.Empty), Index = index });
        var tableFields = fields.Split(',').Select((x, index) => new { FieldName = x, Index = index });

        Dictionary<int, int> mapping = (from dataField in dataFields
                                        let tableField = tableFields.SingleOrDefault(x => string.Equals(dataField.FieldName, x.FieldName, StringComparison.OrdinalIgnoreCase))
                                        where tableField != null
                                        select new { DF = dataField.Index, TF = tableField.Index })
                                       .ToDictionary(c => c.DF, c => c.TF);

        // Test:

        string[] dataFieldsArray = firstLineOfRecord.Split(',');
        string[] tableFieldsArray = fields.Split(',');

        foreach (KeyValuePair<int,int> pair in mapping)
        {
            Console.WriteLine(
                "TableField '{0}' Index {1} has to be mapped to DataField '{2}' Index {3}",
                tableFieldsArray[pair.Value], pair.Value, dataFieldsArray[pair.Key],pair.Key);
        }
    }
}

输出:

TableField 'FrontImages' Index 0 has to be mapped to DataField 'front images' Index 0
TableField 'CurrencyCode' Index 3 has to be mapped to DataField 'Currency Code' Index 1
TableField 'BackImages' Index 1 has to be mapped to DataField 'BackImages' Index 3
TableField 'Domination' Index 2 has to be mapped to DataField 'Domination' Index 4

答案 3 :(得分:-1)

这是使用LINQ查询执行此操作的方法:

string firstLineOfRecord = "front images,Currency Code,Date,BackImages,Domination";

string[] fieldArrayRecord = firstLineOfRecord.Split(',')
                                             .Select(x => x.Replace(" ", string.Empty))
                                             .ToArray();
// Test it, prints True.
fieldArrayRecord.Contains("FrontImages", StringComparer.OrdinalIgnoreCase)

请注意,这将替换这些字母之间的任何空格,并会改变给定的fieldArrayRecord