Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how can i get difference between contents of two strings??
for eg

string test1 ="a,b,c,d,e";
string test2 ="b,e";

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

any help would be appreciated! :)
Posted

There's no way beyond doing your own comparison. If the string is always in this format, then turn them both into arrays of strings, and then compare between them. Use the split method on the string class to split on the ,
 
Share this answer
 
Comments
sanomama 10-Aug-11 21:18pm    
i have splitted the string on , but couldn't compare the results too. Could would provide some code snippets or link.
Christian Graus 10-Aug-11 22:10pm    
If you have an array, you can use the methods on the array to see if it contains a value. This is a basic programming task.
sanomama 10-Aug-11 22:24pm    
thanks for ur time. i really appreciate it.
It might help you,

C#
class Program
{
    static void Main(string[] args)
    {
        var result = Intersect();
        Console.WriteLine(result);
    }

    private static string Intersect()
    {
        string test1 = "a,b,c,d,e";
        string test2 = "b,e";
        return string.Join(",", test1.ToArray().Where(item => !test2.Contains(item.ToString())).ToArray());
    }
}



:)
 
Share this answer
 
v2
Comments
sanomama 10-Aug-11 21:58pm    
i couldn't get item after where in above code.
what am i missing? :(
Mohammad A Rahman 10-Aug-11 22:01pm    
Could you please explain bit more. :)
sanomama 10-Aug-11 22:05pm    
var result = string.Join(",", test1.ToArray().Where(item =>
in above code,
where(items =>

items is a keyword? or it sud be defined somewhere??
Mohammad A Rahman 10-Aug-11 22:06pm    
No You dont, the type for item will infer by compiler on compile time. Just run the code you will get output.:)
sanomama 10-Aug-11 22:08pm    
i tried but the items didn't appear
i'm trying in windows form.
do i have to add any namespace?

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