我如何找出两个琴弦之间的区别 [英] how can i find out the difference between two strings
本文介绍了我如何找出两个琴弦之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我有两个字符串,我想得到两个字符串的内容之间的区别?
例如,
Dear all,
I have two strings, I want to get difference between contents of two strings??
for example,
string one ="a,b,c,d,e";
string two ="b,e";
现在我想要两个字符串之间的区别为"a,c,d"
谢谢所有受尊敬的程序员.
now i want difference between two strings as "a,c,d"
Thank you all the respected programmers.
推荐答案
我认为IEnumerable
,Split
和Join
方法的Except
扩展方法可以按以下方式使用以达到所需的结果.
I think theExcept
extension method ofIEnumerable
,Split
andJoin
methods ofstring
class can be used as follows to achieve the desired result.
string one ="a,b,c,d,e";
string two ="b,e";
string except = string.Join(",",one.Split(',').Except(two.Split(',')).ToArray());
Console.WriteLine (except);
//Output
//a,c,d
以下内容可能对您有帮助,
Following might help you,
using System;
using System.Linq;
namespace ConsoleApplication1
{
class MyClass
{
static void Main(string[] args)
{
string separator =",";
Console.WriteLine(Intersect("a,b,c,d,e", "b,e", separator));
}
private static string Intersect(string item1, string item2, string separator)
{
return string.Join(separator, item1.Where(item => !item2.Contains(item)));
}
}
}
上面的程序将产生以下输出,
The above program will produce the following output,
a,c,d
Press any key to continue . . .
请参阅下面的更多参考文献:
- String.Join [字符串.包含 [如何在C#中工作? -第3部分(详细介绍了C#Linq) [
Please see below for further Ref:
- String.Join[^]
- String.Contains[^]
- How does it work in C#? - Part 3 (C# Linq in detail)[^]
String result = nullstring For each character in string2 If character not in string1 result += character End if End For Display result
这篇关于我如何找出两个琴弦之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- String.Join[^]
查看全文