We help IT Professionals succeed at work.

How to rename duplicated strings in C#

Hakan
Hakan asked
on
Hello,

I'm using below code for getting some strings from excel sheet to use them in another worksheet's table as column headers, but some of strings has duplicated and i'd like to change their naming adding suffix _1, _2, ... , _n

  //Read Parameter Naming
  IEnumerable<ClosedXML.Excel.IXLCell> descList = wsRef.Column(1).CellsUsed().Skip(2).Where(c => !string.IsNullOrWhiteSpace(c.CellRight().Value.ToString()));
  i = 3;
  //Write Table Headers to Sheet
  foreach (ClosedXML.Excel.IXLCell desc in descList) { ws.Cell(1, i).Value = desc.CellRight().Value; i += 1; }

Open in new window


And this is my table column headers;

General
Extrusion by
Contour profile
Input by
SU2 file
Interpolation
Approximation parameter
Extrusion axis
Depth
Cross section
Input by
SU2 file
Input Function
Material side

Now as you can see some of them duplicated values and i'd like to change namings like below;

General
Extrusion by
Contour profile
Input by
SU2 file
Interpolation
Approximation parameter
Extrusion axis
Depth
Cross section
Input by_1
SU2 file_1
Input Function
Material side

Any help would be grateful. Thank you.
Comment
Watch Question

NorieAnalyst Assistant

Commented:
Hakan

Probably not the most efficient way but how about something like this.
   var myList = new List<string> { "General","Extrusion by","Contour profile","Input by","SU2 file","Interpolation","Approximation parameter","Extrusion axis","Depth","Cross section","Input by","SU2 file","Input Function","Material side" };
    var myHeaders = new List<String> {};

    Dictionary <string, int> headers = new Dictionary <string, int>();

    foreach( var heading in myList){

      if(!headers.ContainsKey(heading)){
        headers.Add(heading, 0);
      }
      else{
        headers[heading] = headers[heading] + 1;
      }

      if(headers[heading]!=0){
        myHeaders.Add(heading + "_"+headers[heading]);
      }
      else{
        myHeaders.Add(heading);
      }

    }

    myHeaders.ForEach(i => Console.Write("{0}\n", i));

Open in new window

Developer
Distinguished Expert 2019
Commented:
You could also use LINQ -
using System;
using System.Linq;

namespace EE_Q29171608
{
    class Program
    {
        static void Main(string[] args)
        {
            var names = new[] { "General", "Extrusion by", "Contour profile", "Input by", "SU2 file", "Interpolation", "Approximation parameter", "Extrusion axis", "Depth", "Cross section", "Input by", "SU2 file", "Input Function", "Material side" };
            var headers = (from name in names.Select((x, i) => new { Name = x, Index = i })
                           group name by name.Name into columns
                           from columnn in columns.Select((x, i) => new { Header = x, Index = i })
                           orderby columnn.Header.Index
                           select $"{columnn.Header.Name}{(columnn.Index == 0 ? "" : $"_{columnn.Index}")}");

            foreach (var header in headers)
            {
                Console.WriteLine(header);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG-saige-