ASP.NET - compare string value - Asked By msakt on 15-Jun-12 03:41 AM

string s = 1,2,3,4,5,6,7,8

string s1 = 1

here i check s and s1 and remove 1 from s

output is s2 = 2,3,4,5,6,7,8
Chintan Vaghela replied to msakt on 15-Jun-12 04:13 AM

Hi Frndz,

 

Functionality:  Remove Duplicate Data from Two String

 

Convert string to string array ( Use Split)

 

After then using LINQ concept Remove Duplicate value and Get Unique Value

 

var result = (arrstr.Concat(arrstr1)).GroupBy(g => g.ToString()).Where(g => g.Count() == 1).Select(x => x.Key).ToList();

 

 

Logic   :

 

string S = "1,2,3,4";

        string S1 = "1";

        string[] arrstr = S.Split(',');

 

        string[] arrstr1 = S1.Split(',');

 

 

        var result = (arrstr.Concat(arrstr1)).GroupBy(g => g.ToString()).Where(g => g.Count() == 1).Select(x => x.Key).ToList();

 

 

        string S2 = "";

        foreach (var item in result)

        {

          S2 = S2 + "," + result;

        }

        S2 = S2.TrimStart(','); 

Hope this helpful!

Thanks

 

 

Chintan Vaghela replied to msakt on 15-Jun-12 07:14 AM

Hi Frndz,

 

Functionality:  Remove second array list same item from first array list

 

First convert string to List<string>

List<string> arrstr = S.Split(',').ToList();

 

 

After then using RemoveAll function remove duplicate item of first List

arrstr.RemoveAll(x => arrstr1.Contains(x));

 

 

Afret using String.Join function concate list<string > into string

String.Join(",", arrstr.ToArray());

 

 

 

Full Logic     :

 

string S = "1,2,3,4";

        string S1 = "1";

        List<string> arrstr = S.Split(',').ToList();

        List<string> arrstr1 = S1.Split(',').ToList();

        arrstr.RemoveAll(x => arrstr1.Contains(x));

        string S2 = String.Join(",", arrstr.ToArray());

Hope this helpful!

Thanks

 

 

 

DL M replied to msakt on 15-Jun-12 08:17 AM
About string comparison function you will get here

http://www.ezineasp.net/post/ASP-Net-C-sharp-String-Compare-Function.aspx