【问题标题】:C# - Find and remove substring from a list of stringsC# - 从字符串列表中查找和删除子字符串
【发布时间】:2022-04-30 14:24:23
【问题描述】:

我有一个不好的单词列表。如果字符串包含坏词列表中的任何项目/项目,我需要从字符串中删除该坏词。

List<string> badWordList = new List<string> { "email:", "index", "mobile:", "fax:", "web" };

我可以搜索字符串但无法删除。请帮帮我...我尝试了以下代码:

string myText = "email:abc@gmail.com";
if (badWordList.Any(w => myText.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0))
{
    // How to remove
}

以下是预期输出的输入集:

  1. i/p- 电子邮件:abc@gmail.com

    o/p - abc@gmail.com

  2. i/p- Jack F. 手机:89788987

    o/p- Jack F. 89788987

  3. i/p- Jack F. 电子邮件:t@p.c 手机:65777 WEB

    o/p- Jack F. t@p.c 65777

我更喜欢非正则表达式的方法。感谢您的帮助。

【问题讨论】:

  • 正则表达式有问题吗?这总是更快更方便。
  • myText = myText.Replace(badWordList.First(i =&gt; myText.IndexOf(w, StringComparison.OrdinalIgnoreCase) &gt;= 0), ""); 通常,如果这要处理用户输入,请确保它不会。
  • @Kangkan:正则表达式没问题。
  • @PLB 答案不应该作为评论,无论答案多么简单,它就是一个答案。
  • @PhilipGullick 这不是一个解决方案,因为它在很多情况下会失败,对未来的读者没有帮助。

标签: c# .net string


【解决方案1】:

您可以迭代坏词并将其删除:

foreach (string badWord in badWordList) {
    myText = myText.Replace(badWord, string.Empty);
}

如果您需要不区分大小写的解决方案,您可以使用带有StringComparison 参数的重载:

myText = myText.Replace(badWord, string.Empty, StringComparison.OrdinalIgnoreCase);

注意:这个答案的早期版本,它已经很老了,这个问题的一些 cmets 建议使用正则表达式来进行不区分大小写的字符串替换(参见下面的示例) .
我认为现在StringComparison 存在过载,最好只使用它。

string myText = "EMAIL:abc@gmail.com";
Regex badWords = new Regex("email:|index|mobile:|fax:|web", RegexOptions.IgnoreCase | RegexOptions.Compiled);
myText = badWords.Replace(myText, string.Empty);

【讨论】:

    【解决方案2】:

    您可以通过将字符串替换为空字符串来删除它们:

    foreach (var badWord in badWordList)
    {
        myText = myText.Replace(badWord, "");
    }
    

    不幸的是,这是区分大小写的。对于不使用正则表达式的不区分大小写的字符串替换,请参阅Is there a case insensitive string replace in .Net without using Regex?

    您也可以使用正则表达式来做到这一点,在这种情况下,不区分大小写的比较“免费”提供:

    var regex = String.Join("|", badWordList.Select(w => Regex.Escape(w)));
    var myText = Regex.replace(myText, regex, "", RegexOptions.IgnoreCase);
    

    【讨论】:

    • 一开始,我以为我会采用非正则表达式的方法。但是现在,我认为 Regex 会很有帮助。谢谢!
    【解决方案3】:

    string.Empty替换“坏词”的实例:-

    List<string> badWordList = new List<string> { "email", "index:", "mobile:", "fax:", "web" };
    string myText = "email:abc@gmail.com";
    
    foreach (string s in badWordList)
    {
        myText = myText.Replace(s,string.Empty);
    }
    

    【讨论】:

    • 我要说的只是IndexOf 检查是不必要的,因为如果.Replace 给它的string 在另一个字符串中不存在,它不会替换任何东西。
    【解决方案4】:
                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Collections;
                namespace WindowMaker
                {
                    class Program
                    {
    
                        static void Main(string[] args)
                        {
                            System.Console.WriteLine("Enter Main string...");
                            String str = System.Console.ReadLine();
                            System.Console.WriteLine("Enter sub string...");
                            String sub = System.Console.ReadLine();
                            Boolean flag;
                            int strlen=sub.Length;
                            int inde = str.IndexOf(sub);
                            while (inde != -1)
                            {
                                inde = str.IndexOf(sub);
                                str=str.Replace(sub,"");
    
                            }
                            System.Console.WriteLine("Remaining string :: {0}",str);
    
                                Console.Read();
                        }
    
                    }
                }
    

    【讨论】:

      【解决方案5】:

      如果区分大小写:

      List<String> badWordList = new List<String> { "email:", "index", "mobile:", "fax:", "web" };
      String myText = "This is a exemple of Email: deleting with index and fax and web";
      
      badWordList.ForEach(bw => myText = myText.Replace(bw, String.Empty));
      

      【讨论】:

        猜你喜欢
        • 2021-06-24
        • 1970-01-01
        • 2015-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多