Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to remove a comma separated value from the string..

suppose I have a string like this

string x="r, v, l, m"

and i want to remove r from the above string, and reform the string like this

string x="v, l, m"

from the above string i want to remove any value that my logic throw and reform the string. it should remove the value and comma next to it and reform the string...


The below is specific to my code.. I want to remove any value that I get from the logic, I want to remove it and comma next to it and reform the string with no empty space on the deleted item.. How can I achieve this?

offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',');
if (offIdColl.Split(',').Contains(OfferID.ToString()))
{
    // here i want to perform that operation.   

}

Tombala, i applied it like this but it doesn't work..it returns true

 if (!string.IsNullOrEmpty(my_Order.CustomOfferAppliedonOrder))
                                {
                                    offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',');
                                    if (offIdColl.Split(',').Contains(OfferID.ToString()))
                                    {
                                        string x = string.Join(",", offIdColl.Split(new char[] { ',' },
    StringSplitOptions.RemoveEmptyEntries).ToList().Remove(OfferID.ToString()));
                                    }
                                }
                            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
272 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just do something like:

List<String> Items = x.Split(",").Select(i => i.Trim()).Where(i => i != string.Empty).ToList(); //Split them all and remove spaces
Items.Remove("v"); //or whichever you want
string NewX = String.Join(", ", Items.ToArray());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...