Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have two strings, I want to get difference between contents of two strings??
for example,
C#
string one ="a,b,c,d,e";
string two ="b,e";

now i want difference between two strings as "a,c,d"

Thank you all the respected programmers.
Posted
Updated 16-May-12 9:48am
v2
Comments
VJ Reddy 16-May-12 10:31am    
Thank you for accepting the solution :)

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.
C#
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
 
Share this answer
 
Comments
Mohammad A Rahman 16-May-12 9:29am    
Good use of Except, my 5 :)
VJ Reddy 16-May-12 10:31am    
Thank you, Rahman :)
Maciej Los 16-May-12 11:14am    
Nice! +5
VJ Reddy 16-May-12 11:31am    
Thank you, losmac :)
Following might help you,

C#
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 . . .


Please see below for further Ref:


Hope it helps :)
 
Share this answer
 
v3
Comments
VJ Reddy 16-May-12 8:37am    
Good answer. 5!
Mohammad A Rahman 16-May-12 9:28am    
Thanks VJ :)
Maciej Los 16-May-12 11:15am    
Good answer and useful links! My 5!
Mohammad A Rahman 16-May-12 11:43am    
Thanks Iosmac :)
String result = nullstring
For each character in string2
    If character not in string1
        result += character
    End if
End For
Display result
 
Share this answer
 
string one = "a,b,c,d,e,u";
        string two = "b,e,z,t";
        int key=0;
        StringBuilder sb = new StringBuilder();

        string[] one_arr = one.Split(new char[] { ',' });
        string[] two_arr = two.Split(new char[] { ',' });

        foreach (string _one in one_arr)
        {
            key = 0;
            foreach (string _two in two_arr)
            {
                if (_one == _two)
                {
                    key = 1;
                }
            }
            if (key == 0)
            {
                sb.Append(_one + ",");
            }
        }
        foreach (string _two in two_arr)
        {
            key = 0;
            foreach (string _one in one_arr)
            {
                if (_one == _two)
                {
                    key = 1;
                }
            }
            if (key == 0)
            {
                sb.Append(_two + ",");
            }
        }
        Response.Write(sb);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900