且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-13 21:58:34

这完全取决于你要做的事情:删除一个绝对值(一个永不改变的东西是微不足道的:

  string  Batches =   ,, H104001002,H104001003,H104001004,H104001005; 
string changed = Batched.Replace( H104001002 );

将执行此操作。

但是......如果字符串没有固定或者可以正确地出现在数据的其他位置,那么这不是一个可行的选择。例如,在此处使用该方法:

  string  Batches =   ,, H104001002,H104001003,H104001002,H104001005 

会给你:

,,, H104001003,,H104001005



有很多方法可以做到:你可以分割字符串,杀死坏位置,并重建它:

  string 批次=   ,, H104001002,H104001003,H104001004,H104001005; 
string [] parts = Batches.Split(' 跨度>);
parts [ 2 ] = ;
string changed = string .Join( ,parts);



或者你可以使用正则表达式:

  string  Batches =   ,, H104001002,H104001003,H104001004,H104001005跨度>; 
string changed = Regex.Replace(批次, @ (?< = \,\,)。+?(?=,) );



但是......我建议您查看使用现有的CSV解析器:快速CSV阅读器 [ ^ ]是一个不错的选择。


您可以尝试使用正则表达式(或正则表达式)。在您的情况下,代码将是:



 使用系统; 
使用 System.Text.RegularExpressions;

public class 示例
{
public static void Main()
{
string input = ,H104001002 ,H104001003,H104001004,H104001005
string pattern = H104001002\" 跨度>;
string replacement = ;
Regex rgx = new 正则表达式(模式);
string result = rgx.Replace(input,replacement);

Console.WriteLine( 原始字符串:{0},输入);
Console.WriteLine( 替换字符串:{0},结果);
}
}


  //  使用简单的替换方法。 
string 批次= ,H104001002,H104001003,H104001002,H104001004,H104001005;
string find = H104001002跨度>;
Batches = Batches.Replace(find, );


// 使用Array / List替换它,这将删除空项目
列表< string> lst = new List< string>();
var array = Batches.Split( new char [] {' ,'},StringSplitOptions.RemoveEmptyEntries);
if (array.Contains(find))
for int i = 0 ; i < array.Length ; i ++)
{
if (array [i]!= find)
lst.Add(array [i]);
}
批次= 字符串 .Join( ,lst);

// 使用Linq
批次= string .Join( ,Batches.Split( new char [] {' ,'},StringSplitOptions.RemoveEmptyEntries)。其中(k = > k!= find)。ToArray() );


string Batches = ",,H104001002,H104001003,H104001004,H104001005";

I have this string and i have to remove "H104001002" from the above string in ASP.Net C#.

What I have tried:

I was thinking to use Contains to find the string and Replace to replace it with ""

It depends exactly what you are trying to do: removing an absolute value (one that never changes) is trivial:
string Batches = ",,H104001002,H104001003,H104001004,H104001005";
string changed = Batched.Replace("H104001002", "");

will do it.
But...if string isn't "fixed" or can appear in other places in the data correctly, then that isn't a viable alternative. For example, usign that method on this:

string Batches = ",,H104001002,H104001003,H104001002,H104001005"

Would give you:

",,,H104001003,,H104001005"


There are a number of ways to do it: you could split the string, kill the "bad" location, and rebuild it:

string Batches = ",,H104001002,H104001003,H104001004,H104001005";
string[] parts = Batches.Split(',');
parts[2] = "";
string changed = string.Join(",", parts);


Or you could use a Regex:

string Batches = ",,H104001002,H104001003,H104001004,H104001005";
string changed = Regex.Replace(Batches, @"(?<=\,\,).+?(?=,)", "");


But...I'd suggest that you look at using an existing CSV parser: A Fast CSV Reader[^] is a good choice.


You could try to use regular expressions (or regex). In your case the code will be this:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = ",,H104001002,H104001003,H104001004,H104001005"
      string pattern = "H104001002";
      string replacement = "";
      Regex rgx = new Regex(pattern);
      string result = rgx.Replace(input, replacement);

      Console.WriteLine("Original String: {0}", input);
      Console.WriteLine("Replacement String: {0}", result);                             
   }
}


// using simple Replace method.
     string Batches = ",,H104001002,H104001003,H104001002,H104001004,H104001005";
     string find = "H104001002";
     Batches = Batches.Replace(find, "");


     // using Array/List to replace it, this will remove the empty items
     List<string> lst = new List<string>();
     var array = Batches.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
     if(array.Contains(find))
         for (int i = 0; i < array.Length; i++)
         {
             if (array[i] != find)
                 lst.Add(array[i]);
         }
     Batches = string.Join(",", lst);

     // using Linq
    Batches= string.Join(",", Batches.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(k => k != find).ToArray());