简体   繁体   中英

How can we map a string with case sensitive?

I have my code like,

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

How can i map, the "front Images" with "FrontImages".

As iam the beginner i dont know how to solve this.Kindly tell me how to achieve this.

For such a fuzzy match, you first need to identify the valid identifiers to ignore (in this case a space).

You could do something like this: You strip out all those identifiers. Then compare case and culture insensitive.

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 */ }

This is a little hacky, but you get the idea. You'd better use a custom implementation of a StringComparer that just ignores the characters to strip out.

As I understand from your question, your problems are spaces and case sensitivity so you can use

fieldArrayList[i].Replace(" ","").ToLower() == 
    fieldArrayRecord[j].Replace(" ","").ToLower()
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);
        }
    }
}

Output:

0
1
3
4

Or using a dictionary:

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

Output:

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

Here's a way to do it with a LINQ query:

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)

Note this will replace any white space between those letters and will alter the given fieldArrayRecord .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM