-1

So this is the example:

string one = "Test123";

string two = "123Test12345"

I want to compare the strings and replace it the characters by using indexes. So the string "two" becomes string "one" with placing 123 in front of the Test123 and 45 behind the Test123. Is there anyway to that?

Llazar
  • 3,167
  • 3
  • 17
  • 24
  • If the end result is to reproduce the content of string `two`, why is `one = two;` (or vice versa) not an option? – Lasse V. Karlsen Oct 13 '18 at 18:43
  • If you are replacing all the different things, just assign string 2 to string 1 – Ňɏssa Pøngjǣrdenlarp Oct 13 '18 at 18:44
  • This is because multiple clients are working on one document and it needs to update otherwise the server has to send the whole document again and again – Digitalgamer Oct 13 '18 at 18:46
  • 2
    I have a nuget package for this, called difflib - https://www.nuget.org/packages/difflib/2017.7.26.1241 - you can use that to work out the differences, but it might not be optimal if your document gets really big in size. – Lasse V. Karlsen Oct 13 '18 at 18:50
  • `String.IndexOf` will find the starting index of the match. You know the length. Then it's a simple matter of playing with `string.SubString` and gluing things back together – Flydog57 Oct 13 '18 at 18:50
  • @LasseVågsætherKarlsen i can only find things about ratio with that library and that isnt what i need right now – Digitalgamer Oct 13 '18 at 19:11
  • No, the library would be able to tell you exactly which bits were added, were deleted, modified, or stayed the same. – Lasse V. Karlsen Oct 13 '18 at 19:12
  • Doing a Diff is a non-trivial thing. Very hard unless you can add limiting conditions and rules! See [here](https://stackoverflow.com/questions/24887238/how-to-compare-two-rich-text-box-contents-and-highlight-the-characters-that-are/24970638?s=1|27.1951#24970638) for a discouraging example – TaW Oct 13 '18 at 19:16
  • Here is an example with Difflib, code and output: https://gist.github.com/lassevk/caba9a5009871bc57723660e4ff5c5d4 – Lasse V. Karlsen Oct 13 '18 at 19:18

1 Answers1

-1

You can compare simply by using Regular expression

string outputText = Regex.Match(two, one,RegexOptions.IgnoreCase).Value;
Console.WriteLine(outputText) // "Test123"
Community
  • 1
  • 1