String difference in c# .net

gpinfotech
gpinfotech used Ask the Experts™
on
Hi,

I have two strings like string1="1,2,3,4,5,6";    and string2="4,5,6";  i need to find the difference of strings and, i want store it in another string i.e the difference is "1,2,3".


Please help me.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try this:-

 
string string1 = "1,2,3,4,5,6";
       string  string2 = "4,5,6";
       string string3 = string1.Replace(string2, string.Empty);

Open in new window

here string3 is the required string.
Kalpesh ChhatralaSoftware Consultant

Commented:
try this function

        public static string ReturnDiff(string firstString, string scndString)
        {
            StringBuilder newStb = new StringBuilder();
            if (firstString != null && scndString != null)
            {
                for(int i=0;i<firstString.Length;i++)
                {
                    if (!scndString.Contains(firstString[i]))
                        newStb.Append(firstString[i]);
                }
            }
            return newStb.ToString();
        }

Open in new window

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial