7

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()));
                                    }
                                }
                            }
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166
  • 1
    And afterwards it must be a string again? – H H May 21 '13 at 13:03
  • 1
    not clear, can you show an example of what you want ? – Cybermaxs May 21 '13 at 13:06
  • 1
    what should be the expected output? – chaliasos May 21 '13 at 13:06
  • You really need to explain your requirements better. Are you trying to remove, for example, the `v, ` from the string, resulting in `"r, l, m,"`? – Jim Mischel May 21 '13 at 13:11
  • Is the space part of the comma separated string? ie in the example above remove "r" from the string but also the space before "v"? is the separator actually ", "? Also can you explain the logic about why the original string had a comma at the end and the new one didn't? – Chris May 21 '13 at 13:12

10 Answers10

18

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());
PhonicUK
  • 13,486
  • 4
  • 43
  • 62
3

Something like this?

string input = "r,v,l,m";
string output = String.Join(",", input.Split(',').Where(YourLogic));

bool YourLogic(string x)
{
    return true;
}
I4V
  • 34,891
  • 6
  • 67
  • 79
3
var l = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
l.Remove(OfferID.ToString());
x = string.Join(",", l);

Edit: Sorry, you're right. Remove doesn't return the original list. You need multiple statements. But you don't need to trim the end "," implicitly. You can remove that statement from your code as well as the check to see if the item is there or not. The Remove will take it out if it was found or simply return false if it was not found. You don't have to check existence. So remove the TrimEnd from the first and get rid of the second line below:

offIdColl = my_Order.CustomOfferAppliedonOrder; //.TrimEnd(',');
//if (offIdColl.Split(',').Contains(OfferID.ToString()))
Tombala
  • 1,660
  • 9
  • 11
3

Not quite sure if this is what you mean, but this seems simplest and most readable:

        string x = "r, v, l, m";
        string valueToRemove = "r";
        var result = string.Join(", ", from v in x.Split(',')
                                       where v.Trim() != valueToRemove
                                       select v);

Edit: like Bob Sammers pointed out, this only works in .NET 4 and up.

Bas
  • 1,946
  • 21
  • 38
  • 3
    +1 for clear, neat answer, but worth pointing out that it only works in .net 4+ because string.Join() won't take IEnumerable as an argument in 3.5. – Bob Sammers May 21 '13 at 14:53
2
String input = "r, v, l, m, ";

string itemToReplace = "v, ";

string output = input.Replace(itemToReplace, string.Empty)
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
1
public void string Remove(string allStuff, string whatToRemove)
{
  StringBuilder returnString = new StringBuilder();
  string[] arr = allStuff.Split('');
   foreach (var item in arr){
     if(!item.Equals(whatToRemove)){
     returnString.Append(item);
     returnString.Append(", "); 
    }
   }
  return returnString.ToString();
}
Mateusz
  • 2,287
  • 20
  • 28
1

So you want to delete an item (or replace it with a nother value) and join the string again with comma without space?

string x = "r, v, l, m,";
string value = "v";
string[] allVals = x.TrimEnd(',').Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
// remove all values:
x = string.Join(",", allVals.Where(v => v.Trim() != value));
// or replace these values
x = string.Join(",", allVals.Select(v => v.Trim() == value ? "new value" : v));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

// If you want to remove ALL occurences of the item, say "a" you can use

  String data = "a, b, c, d, a, e, f, q, a";

  StringBuilder Sb = new StringBuilder();

  foreach (String item in data.Split(',')) {
    if (!item.Trim().Equals("a", StringComparison.Ordinal)) {
      if (Sb.Length > 0) 
        Sb.Append(',');

      Sb.Append(item);
    }
  }

  data = Sb.ToString();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Its just single line of code in many ways, two of them are below:

string x = "r,v,l,m";
string NewX = String.Join(",", from i in x.Split(',') where  i != String.Empty && i != "v" select i);

OR

string NewX = String.Join(",", x.Split(',').Select(i => i.Trim()).Where(i => i != String.Empty && i != "v"));
Gaurav
  • 21
  • 1
  • 4
0

Not going about this right. Do you need to keep the string? I doubt you do. Just use a list instead. Can you have duplicates? If not:

offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',').Split(',');

if (offIdColl.Contains(OfferID.ToString()))
{
    offIdColl.Remove(OfferID.ToString());
}
Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39