[英]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
. 例如,如果弦一是my car is bad
,弦二是my horse is good
,它将返回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]`. 但是我收到错误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]
. 您没有看到错误-您看到的是result
类型的标准名称,即System.Linq.Enumerable+d_921[System.String]
。 This is the default behavior for ToString()
, unless it is overridden in an inheriting class. 这是ToString()
的默认行为,除非它在继承类中被覆盖。 See Object.ToString() . 参见Object.ToString() 。
To show the results of the intersection, you can use String.Join
, like this: 要显示相交的结果,可以使用String.Join
,如下所示:
Test = String.Join(" ", results);
Which would produce my is
. 哪个会产生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[]'
. 上面的几行生成了一个Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string[]'
。 Adding .ToArray()
is one way to resolve this. 添加.ToArray()
是解决此问题的一种方法。 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 工作提琴: 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. 发生这种情况基本上是因为您累了以获得IEnumerable<string>
的string
表示形式,并且这是完全正常的,因为它返回了object
类的默认ToString()
。 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! 还要注意,不需要获取数组的Distinct
结果,因为Intersect
是一个set操作,自然会返回Distict
结果!
This program gets intersection of 2 strings (assuming duplication is allowed) 该程序获取2个字符串的交集(假设允许重复)
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.