-1

I have list like this:

List<string> myList = new List<string>() 
{
    "AS2258B43C014AI9954803",
    "AS2258B43C014AI9954603",
    "AS2258B43C014AI9954703",
    "AS2258B43C014AI9954503",
    "AS2258B43C014AI9954403",
    "AS2258B43C014AI9954203",
    "AS2258B43C014AI9954303",
    "AS2258B43C014AI9954103",
 };

I want to output something format is sameString+diffString0\diffString1\diffString2.... like this "AS2258B43C014AI9954803\603\703\503\403\203\303\103"

what should I do?

3 Answers3

0

Alphanumeric sorting using LINQ

Create static method for pads

 public static string PadNumbers(string input)
    {
        return Regex.Replace(input, "[0-9]+", match => match.Value.PadLeft(10, '0'));
    }

then call it

 var result = myList.OrderBy(x => PadNumbers(x));

example

after that you can find the difference

0

The simplest solution is to create a function like this :

public static string Output(List<String> ListString)
{
  string sameString = ListString.First().Substring(0, 18);
  string output = sameString;
  foreach (var item in ListString)
  {
    output += item.Substring(19);
    output += "\\";
  }

  return output.Substring(0, output.Length - 1);
}
0

You can create a method which returns you the difference between to strings:

private static string GetDiff (string s1, string s2)
    {
        int i;
        for(i = 0; i < Math.Min(s1.Length,s2.Length); i++)
        {
            if(s1[i] != s2[i])
            {
                break;
            }
        }
        return s2.Substring(i);
    }

This method iterats until the first character which is different and returns the remaining characters of the second string.

With that method, you can obtain your result with the LINQ query:

string first = myList[0];
string result = first + "/" + string.Join("/", myList.Skip(1).Select(x => GetDiff(first,x)));

Online demo: https://dotnetfiddle.net/TPkhmz

SomeBody
  • 7,515
  • 2
  • 17
  • 33