-3

In C#, how do I compare the characters in two strings. For example, let's say I have these two strings "admin12@3" and "adminb12@3. ",How do I programically return the different letter from these two string?

  • maxm without loops – Riyas Mohammed Nov 01 '16 at 07:17
  • 1
    Get your words in a list format, maybe a List, and then call the Except extension method on the list. This will return the difference – monstertjie_za Nov 01 '16 at 07:18
  • 1
    http://stackoverflow.com/questions/9065536/text-comparison-algorithm – Lucero Nov 01 '16 at 07:18
  • 1
    http://wiki.c2.com/?DiffAlgorithm – Lucero Nov 01 '16 at 07:19
  • @active92 The question you propose as duplicate seems to ask for something different, in that it wants to find words which are in one text but not in the other, regalrdless of position etc. It does not work in single characters. – Lucero Nov 01 '16 at 07:21
  • What is the expected output for your example? Do you want to find all the places where string +1[x] != string2[x]? + Have you tried anything before posting? – Gilad Green Nov 01 '16 at 07:23
  • @Lucero: the proposed (and now marked) duplicate includes several answers, any one of which includes information that should be useful to the OP here, especially the answer that provides references to other resources describing algorithms the OP might have found had they tried to search the web before posting their question. There are _lots_ of ways to address the vaguely stated problem description here, so the answers in the marked duplicate are as good as any others. – Peter Duniho Nov 01 '16 at 07:35
  • If the OP needs more specifics, they need to post a new question, including a good [mcve] that shows what they tried, and a detailed and _precise_ explanation of what exactly that code does, and what they want it to do instead. – Peter Duniho Nov 01 '16 at 07:35

1 Answers1

1

Something, as simple as this could solve your problem. This is not the most efficient piece of code, but you can tweak it to work for you.

static void Main(String[] args)
    {
        var strOne = "abcd";
        var strTwo = "bcd";

        var arrayOne = strOne.ToCharArray();
        var arrayTwo = strTwo.ToCharArray();

        var differentChars = arrayOne.Except(arrayTwo);

        foreach (var character in differentChars)
            Console.WriteLine(character);  //Will print a
    }
monstertjie_za
  • 7,277
  • 8
  • 42
  • 73
  • What if `strTwo` = `'bcda'`? In your code, nothing will be printed, yet the strings are different. – Chris Dunaway Nov 01 '16 at 15:48
  • It's not checked for different strings at the place, but all characters from the entire strings. The question was: "How do I programically return the different letter from these two string?" – SwissCodeMen Aug 18 '20 at 08:12