【问题标题】:How to remove a word say "code" from myArray[1] and NOT the full myArray[1] using C# [duplicate]如何使用 C# 从 myArray[1] 而不是完整的 myArray[1] 中删除单词“code”[重复]
【发布时间】:2021-12-28 03:41:05
【问题描述】:

如何从 myArray[1] 而不是完整的 myArray[1] 中删除单词“code”。

string[] myArray = new string[3];
            myArray[0] = "Hello World";
            myArray[1] = "This is a sample code";
            myArray[2] = "Delete me";

【问题讨论】:

  • 将问题分解成更小的部分。首先,您需要从数组元素中获取当前字符串。然后,您需要修改字符串。最后,您可以将新字符串重新分配给数组元素。这些步骤中的每一个都在 Internet 和 Stack Overflow 上记录了无数次。
  • 您尝试实施了哪些解决方案?什么对他们不起作用?
  • 不确定你在问什么。您是说要查看字符串数组,搜索要删除的文本。如果myArray[i] 不包含搜索文本,请不要理会它。如果它确实包含搜索文本,那么更改 myArray[i] 使其像以前的版本一样引用一个字符串,但删除了搜索文本?如果是这样,我的问题基本上是你想要的算法

标签: c#


【解决方案1】:

使用Replace 方法

myArray[1] = myArray[1].Replace("code", "");

更多关于Replace - https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=net-6.0

【讨论】:

    【解决方案2】:

    您可以使用String.Replace() 将目标单词替换为空字符串:

    string[] myArray = new string[3];
    myArray[0] = "Hello World";
    myArray[1] = "This is a sample code";
    myArray[2] = "Delete me";
    
    foreach(var s in myArray) {
        Console.WriteLine(s);
    }
    
    Console.WriteLine("=================");
    
    // One specific entry
    
    myArray[1] = myArray[1].Replace("code", "");
    
    foreach(var s in myArray) {
        Console.WriteLine(s);
    }
    
    Console.WriteLine("=================");
    
    // All entries (if there were multiple)
    
    for(int i = 0; i < myArray.Length; i++) {
        myArray[i] = myArray[i].Replace("code", "");
    }       
    
    foreach(var s in myArray) {
        Console.WriteLine(s);
    }
    
    Hello World
    This is a sample code
    Delete me
    =================
    Hello World
    This is a sample 
    Delete me
    =================
    Hello World
    This is a sample 
    Delete me
    

    https://dotnetfiddle.net/N0LkKI

    【讨论】:

    • 谢谢,它对我有用。
    【解决方案3】:

    有很多方法可以做到这一点,其中一些已经提到过。这是使用扩展例程的替代解决方案。

    public static class Extensions
        {
            public static void ReplaceText(this string[] array, string word, int index = 0, string newWord = "", bool replaceAll = false)
            {
                if (array?.Length > 0 && word != null)
                {
                    if (replaceAll)
                    {
                        for (int i = 0; i <= array.Length - 1; i++)
                        {
                            // You could check if it contains the 
                            // word before trying to replace it
                            array[i] = array[i].Replace(word, newWord);
                        }
                    }
                    else
                    {
                        if (array.Length >= index)
                            array[index] = array[index].Replace(word, newWord);
                    }
                }
            }
        }
    

    示例用法:

      myArray.ReplaceText("code", 1); //replace just the word at that index
      myArray.ReplaceText("code", replaceAll: true); //replace the word in all index's 
    

    另外值得注意的是,如果可以的话,考虑使用List,它们比数组更容易使用和管理。

    【讨论】:

    • 它也有效,但只有我使用了 array[i].replace()。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 2019-04-11
    • 2020-09-17
    • 2017-06-14
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多