简体   繁体   中英

Replace the string in a list that are same with start string

I don't know if my title is correct.

I have a list

\1925\10\04\issue1
\1925\10\05\issue1
\1925\10\07\issue1
\1925\10\10\issue1
\1925\10\11\issue1
\1925\10\12\issue1
\1925\10\13\issue1
\1925\10\14\issue1
\1925\10\15\issue1
\1925\10\17\issue1
\1925\10\18\issue1
\1925\10\19\issue1

And what i want to do in the list is became

\1925\10\04\issue1
\05\issue1
\07\issue1
\10\issue1
\11\issue1
\12\issue1
\13\issue1
\14\issue1
\15\issue1
\17\issue1
\18\issue1
\19\issue1

I need it to be dynamic.

There may be instance that i have a list like this

\1925\10\04\issue1
\1925\10\04\issue2
\1925\10\04\issue3
\1925\10\04\issue4

And the output is like this

\1925\10\04\issue1
\issue2
\issue3
\issue4

So far i'm using diff match patch.

https://github.com/google/diff-match-patch/wiki/Language:-C%23

And here is my code.

diff_match_patch dmp = new diff_match_patch();
            var d = dmp.diff_main(@"\1925\10\14\issue1", @"\1925\10\05\issue1");

            //dmp.diff_cleanupEfficiency(d);

            foreach (var item in d)
            {
                Console.WriteLine($"text {item.text} operation {item.operation}");
            }

But is there a better way of doing this? or faster way

assuming you have the input as List<string> input then this code should work:

  var splittet = input.Select(i => i.Split("\\".ToCharArray(),  StringSplitOptions.RemoveEmptyEntries));

  Action<string[], int> print = (string[] lst, int index) => Console.WriteLine("\\" + string.Join("\\", lst.Skip(index)));

  splittet.Aggregate(new string[] { },
    (common, item) =>
    {
      var index = Enumerable.Range(0, Math.Min(common.Length, item.Length)).FirstOrDefault(i => common[i] != item[i]);
      print(item, index);
      return item;
    }
    );

So given the input

var input = new List<string> { @"\1925\10\04\issue1",
@"\1925\10\05\issue1",
@"\1925\10\07\issue1",
@"\1925\10\10\issue1",
@"\1925\10\11\issue1",
@"\1925\10\12\issue1",
@"\1925\10\04\issue1",
@"\1925\10\04\issue2",
@"\1925\10\04\issue3",
@"\1925\10\04\issue4"};

this is the output:

\1925\10\04\issue1
\05\issue1
\07\issue1
\10\issue1
\11\issue1
\12\issue1
\04\issue1
\issue2
\issue3
\issue4

Some explanation:

First instead of working with a list of string, I split it up to a list of tokens.

Then I defined some print-action. you could instead add the result to an output-list or do whatever. In this case, it is just writing to console

then the list is aggregated. the aggreagtor starts with an empty string-array. then it tries to find the first index, where the first item differs from the emtpy list and prints all parts from this index on. and then the first index is returned to the aggregator. The aggregator then compares this first item with the second item, finds the first index where the parts differ and prints the parts from there on etc etc.

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