String Functions in .NET

Srinivas_Vengala
Srinivas_Vengala used Ask the Experts™
on
I have 2 strings
string s1 = "Mr,Wilson,is,good";
string s2 = "Mr,is";
I need to get the difference between the 2 string like
Output should be - Wilson,good.
Can anyone tell me if there is any in built function for getting this...
I have used s1.Remove(s1.IndexOf(s2), s2.Length); but it will give the output only if the order of
strings in the 2nd string also is same as 1st string.
Words are comma delimited.

Thanks.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There is no built in function to do this, would you like help writing something custom ?

Commented:

string[] words1 = s1.Split(',');

string[] words2 = s2.Split(',');

string output = "";

for(int i =0; i < words1.length; i++)
{
 bool inList = false;

for(int j =0; j < words2.length; j++)
{
if(words1[i] == words2[j])
{
inList = true;
}
}

if(!inList)
{
output += "," + words1[i];
}

}
nothing built in that you can use out of the box.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.


	string s1 = "Mr,Wilson,is,good";
	string s2 = "Mr,is";

        string[] words = s2.Split(' ');
        foreach (string word in words)
        {
            s1.Remove(s1.IndexOf(word), word.Length)
        }

Open in new window

You can look at the strings as 2 sets or chars, set s1 and set s2, and you want s1-s2 U s2-s1.

I would split the strings by "," (there is a function called split).
and put these strings in List<string> (so I'd have the function "contains").

for each word in s1, I would look in s2. if the word is not found, add to List<string> different.
for each word in s2, I would look in s1. if the word is not found, add to List<string> different.

ta-da.
nice roshmon -- simple and clean.

but, one thing



 string s1 = "Mr,Wilson,is,good";
        string s2 = "Mr,is";

        string[] words = s2.Split(' ');
        foreach (string word in words)
        {
            s1 = s1.Remove(s1.IndexOf(word), word.Length)//remove returns a string
        }
I mean
 string s1 = "Mr,Wilson,is,good";
        string s2 = "Mr,is";

        string[] words = s2.Split(',');
        foreach (string word in words)
        {
            s1.Remove(s1.IndexOf(word), word.Length)
        }

Open in new window

ok -- there were 2 things =)
yes, thanks davis
Consultant, Trainer
Commented:
answer is the resultstr:

string s1 = "Mr,Wilson,is,good";
            string s2 = "Mr,is";

            string[] s1arr = s1.Split(Convert.ToChar(","));
            string[] s2arr = s2.Split(Convert.ToChar(","));
            string resultstr = string.Empty ;
            bool exists = false;

            foreach (string strTemp1 in s1arr)
            {
                exists = false;
                foreach (string strTemp2 in s2arr)
                {
                    if (strTemp1 == strTemp2)
                    {
                        exists = true;
                    }
                }
                if (!exists)
                    resultstr += strTemp1 + ",";
            }
            resultstr = resultstr.Remove(resultstr.Length-1);
            this.Text = resultstr;

Open in new window

thanks davis for the correction
        string s1 = "Mr,Wilson,is,good";
        string s2 = "Mr,is";

        string[] words = s2.Split(',');
        foreach (string word in words)
        {
            s1 = s1.Remove(s1.IndexOf(word), word.Length)
        }

Open in new window

i like roshmon's idea but you might want to make another change


 string s1 = "Mr,Wilson,is,good";
        string s2 = "Mr,is";

        string[] words = s2.Split(', ');
        foreach (string word in words)
        {
            s1 = s1.Remove(s1.IndexOf(word) - 1, word.Length)//-1 from the index so you can delete the preceding comma otherwise you will end up with a bunch of them (possibly).
        }
then of course you will have to check to see if the index exists -- could be the first.


 string s1 = "Mr,Wilson,is,good";
        string s2 = "Mr,is";

        string[] words = s2.Split(', ');
        foreach (string word in words)
        {
            s1 = s1.Remove(s1.IndexOf(Word) != 0 ? s1.IndexOf(word) - 1: s1.IndexOf(word) , word.Length)
        }

Commented:
I'm pretty sure what I posted is the same logic as the accepted solution only I was first??

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