Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Plz Help I got two string values
string a= "1,3,2,4,5";
b = "3,4";
c = a-b;
c = "1,2,5";
remove second string value from first string
and I want the result as "1,2,5"
Posted
Updated 11-Sep-14 1:14am
v5
Comments
ErBhati 11-Sep-14 6:34am    
your question is nor clear.
Bh@gyesh 11-Sep-14 6:36am    
Can you please more elaborate on this?
[no name] 11-Sep-14 6:39am    
Okay so remove the "3,4" from the first string.

C#
var s1 = "1,2,3,4,5";
var s2 = "3,4";
s1 = s1.Replace(s2, string.Empty).Replace(",,",",");


You are using the "Replace" method twice here, the first removes the second string, the second looks for double , and replaces it with a single ,
 
Share this answer
 
Comments
George Jonsson 11-Sep-14 7:18am    
In this case what happens if s2 is changed to "2,3,4"?
Pheonyx 11-Sep-14 7:19am    
You'd get 1,5 :-)
George Jonsson 11-Sep-14 8:20am    
You are absolutely right.
My bad. :P
Sounds like Homework. Try this

C#
char[] delimiterChars = { ',' };

string a = "1,3,2,4,5";
string b = "3,4";

string[] A = a.Split(delimiterChars);
string[] B = b.Split(delimiterChars);

List<string> C = A.Except<string>(B).ToList();
string c = string.Join(",", C);
 
Share this answer
 
Comments
Matt T Heffron 11-Sep-14 12:03pm    
+5
try below code

C#
for (int i = 0; i < s1.Length; i++)
{
    int count = 0;
    if (s1[i] != ',')
    {

        for (int j = 0; j < s2.Length; j++)
        {
            if (s1[i].ToString() == s2[j].ToString())
            {
                count++;
            }
        }
        if (count == 0)
            s3 += s1[i].ToString() + ',';
    }
}
s3.Remove(s3.Length - 1);
 
Share this answer
 
Try this Code..

C#
string s1 = "1,2,3,4,5";
        string s2="2,3";
        var res1 = s1.Split(',');
        var res2 = s2.Split(',');
        string res = "";
        for(int i=0;i<res1.count();i++)>
        {
            for(int j=0;j<res2.count();>
            {
                if (res1[i] == res2[j])
                {
                    break;
                }
                else
                {
                    if ((j == (res2.Count() - 1)))
                    {

                        res = res + res1[i].ToString() + ",";
                    }
                }
            }
        }
 
Share this answer
 
v2

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