简体   繁体   中英

Showing the intersection between two strings

I am trying to find the intersection between two strings. For example, if string one was my car is bad , and string two was my horse is good , it would return my is . This is my code:

  public static string intersection2(string x1, string x2)
{
  string[] string1 = x1.Split(' ');
string[] string2 = x2.Split(' ');
string[] m = string1.Distinct();
 string[] n = string2.Distinct();
  string Test;
 var results = m.Intersect(n,StringComparer.OrdinalIgnoreCase);
 Test = results.ToString();
   return Test;

}

But I get the error System.Linq.Enumerable+d__92 1[System.String]`. Could someone explain what's going on, and how I can fix it?

You're not seeing an error - what you are seeing is the fully qualified name of the type of result , which is System.Linq.Enumerable+d_921[System.String] . This is the default behavior for ToString() , unless it is overridden in an inheriting class. See Object.ToString() .

To show the results of the intersection, you can use String.Join , like this:

Test = String.Join(" ", results);

Which would produce my is .

Note that your code as posted wouldn't compile:

string[] m = string1.Distinct();
string[] n = string2.Distinct();

The above lines generated a Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string[]' . Adding .ToArray() is one way to resolve this. Full code below:

public static string intersection2(string x1, string x2)
{

    string[] string1 = x1.Split(' ');
    string[] string2 = x2.Split(' ');
    string[] m = string1.Distinct().ToArray();
    string[] n = string2.Distinct().ToArray();
    string Test;
    var results = m.Intersect(n, StringComparer.OrdinalIgnoreCase);
    Test = String.Join(" ", results);
    return Test;
}

Your logic is okay, you can make it work by fixing a little bit

public static string intersection2(string x1, string x2)
{
    string[] string1 = x1.Split(' ');
    string[] string2 = x2.Split(' ');
    var m = string1.Distinct();
    var n = string2.Distinct();

    var results = m.Intersect(n, StringComparer.OrdinalIgnoreCase);
    //The result is a list of string, so we just have to concat them
    var test = " ";
    foreach(var k in results) test += k + " ";
    return test;
}

Working fiddle: https://dotnetfiddle.net/joO0d9

That's basically happened because you tired to get the string representation of an IEnumerable<string> and it's completely normal because it returns the default ToString() of object class. So in order to fix that you should somehow make your own string representation, which will be st like this:

string[] string1 = x1.Split(' ');
string[] string2 = x2.Split(' ');
string Test;
var results = string1.Intersect(string2, StringComparer.OrdinalIgnoreCase);
Test = string.Join(" ", results);

also note that there is no need to get Distinct result of your arrays, because Intersect is a set operation and naturally returns the Distict result!

This program gets intersection of 2 strings (assuming duplication is allowed)

public String getIntersection(String s1, String s2) {
    String common = "";
    for (int i = 0; i < s1.length(); i++) {
        for (int j = 0; j < s2.length(); j++) {
            if (s1.length() == 0 || s1.length() == 0)
                return common;
            if (s1.charAt(i) == s2.charAt(j)) {
                common = common + s1.charAt(i);
                // delete character from each if there is a match
                s1 = s1.substring(0, i) + s1.substring(i + 1);
                s2 = s2.substring(0, j) + s2.substring(j + 1);
                i = -1;
                break;
            }
        }
    }
    return common;
}

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