c# - How to remove comma separated duplicated string values and get last two values -
c# - How to remove comma separated duplicated string values and get last two values -
i want remove comma separated duplicate string values :
string str = "2,4,3,12,25,2,4,3,6,2,2,2";
and want output this:
string str1 = "6,2";
please tell how i'll self can't solve this
a wild ride linq. there improve way, first 1 think of.
string str = "2,4,3,12,25,2,4,3,6,2,2,2"; list<string> uniques = str.split(',').reverse().distinct().take(2).reverse().tolist(); string newstr = string.join(",", uniques); console.writeline(newstr);
split string @ comma sequence apply reverse op, 2 2 2 6 .... 4 2 apply distinct, 2,6,3,4,25,12 take first 2 elements (2,6) reverse them 6,2 join in new string comma sep. c# asp.net string
Comments
Post a Comment