简体   繁体   English

如何从字符串中删除逗号分隔值?

[英]How to remove comma separated value from a string?

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 我想从上面的字符串中删除r,并像这样重写字符串

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 Tombala,我这样应用它但它不起作用..返回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()));
                                    }
                                }
                            }

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());

Something like this? 像这样的东西?

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

bool YourLogic(string x)
{
    return true;
}
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. 删除将在找到它时将其取出,或者如果未找到则将其返回false。 You don't have to check existence. 你不必检查存在。 So remove the TrimEnd from the first and get rid of the second line below: 所以从第一行中删除TrimEnd并删除下面的第二行:

offIdColl = my_Order.CustomOfferAppliedonOrder; //.TrimEnd(',');
//if (offIdColl.Split(',').Contains(OfferID.ToString()))

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. 编辑:像Bob Sammers所指出的那样,这只适用于.NET 4及更高版本。

String input = "r, v, l, m, ";

string itemToReplace = "v, ";

string output = input.Replace(itemToReplace, string.Empty)
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();
}

So you want to delete an item (or replace it with a nother value) and join the string again with comma without space? 所以你想要删除一个项目(或用一个nother值替换它)并再次用逗号连接字符串没有空格?

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));

// If you want to remove ALL occurences of the item, say "a" you can use //如果要删除项目的所有出现,请说“a”可以使用

  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();

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());
}

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM