Thursday, December 3, 2009

Compare and extract difference string from two string[].

Hi,

In my one requirement i have to compare difference between two string[] through which i can justify missing headers.

.net have Dictionary class which i have used to solve my requirement.

below is code snippet how,

385 Dictionary<string, int> diff = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);

386 foreach (string s in _strHeadersNames)

387 diff[s] = 1;

388 string[] strvalues = IsColumnHeadersExist(_strHeadersNames, strCsvFilePath).Split(',');

389 if (strvalues.Length > 1)

390 {

391 foreach (string s in strvalues)

392 {

393 int value;

394 if (diff.TryGetValue(s, out value) && value != 2)

395 diff[s] = 3;

396 else

397 diff[s] = 2;

398 }

399 foreach (KeyValuePair<string, int> pair in diff)

400 if (pair.Value == 1)

401 _strDiff = string.Concat(_strDiff, pair.Key, ",");


I hope above will be useful to you.

Happy Coding:-)

1 comment: