我如何找出两个琴弦之间的区别 [英] how can i find out the difference between two strings

查看:92
本文介绍了我如何找出两个琴弦之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有两个字符串,我想得到两个字符串的内容之间的区别?
例如,

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.

推荐答案

我认为IEnumerableSplit Join 方法的Except 扩展方法可以按以下方式使用以达到所需的结果.
I think the Except extension method of IEnumerable, Split and Join methods of string 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 . . .



请参阅下面的更多参考文献:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆