293

I have a string called "hello world"

I need to replace the word "world" to "csharp"

for this I use:

string.Replace("World", "csharp");

but as a result, I don't get the string replaced. The reason is case sensitiveness. The original string contains "world" whereas I'm trying to replace "World".

Is there any way to avoid this case sensitiveness in string.Replace method?

Liam
  • 27,717
  • 28
  • 128
  • 190
Sandeep
  • 5,581
  • 10
  • 42
  • 62
  • 5
    Here you find similiar problem: [Is there an alternative to string.Replace that is case-insensitive?](http://stackoverflow.com/questions/244531/is-there-an-alternative-to-string-replace-that-is-case-insensitive) – Michał Kuliński Jun 08 '11 at 08:23
  • 3
    Possible duplicate of [Is there an alternative to string.Replace that is case-insensitive?](https://stackoverflow.com/questions/244531/is-there-an-alternative-to-string-replace-that-is-case-insensitive) – Jon Schneider Sep 01 '17 at 18:13
  • It is a shame that Microsoft did not implement such a basic functionality into the framework! – Elmue Oct 26 '20 at 17:41
  • 3
    For .NET 5 and recent versions of .NET Core, just use the overload of string.Replace that takes a StringComparison as described in https://stackoverflow.com/a/64677285/ – Sepia Jun 02 '21 at 12:06

20 Answers20

389

You could use a Regex and perform a case insensitive replace:

class Program
{
    static void Main()
    {
        string input = "hello WoRlD";
        string result = 
           Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
        Console.WriteLine(result); // prints "hello csharp"
    }
}
John Smith
  • 7,243
  • 6
  • 49
  • 61
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 25
    Not works with **Regex language elements**, so it's not universal method. Steve B's answer is correct. – AsValeO Aug 04 '15 at 07:11
  • 1
    So you better don't write `hello. world?` or anything else containing regex operators. – Sebastian Mach Mar 12 '18 at 15:37
  • 13
    Just in case anyone wasn't inclined to read further, this was the accepted answer in 2011 and has a huge number of votes. This works fine if you only have to replace alphanumeric. However, if you have to replace any punctuation characters you can get into big trouble. Oleg Zarevennyi's answer is superior, but has only a small number of votes because it was posted in 2017. – Tony Pulokas Feb 28 '19 at 19:12
  • Replace with $$ doesn't work. If your replacement can contain $$ then go with Oleg's solution. – Steven Spyrka Apr 09 '21 at 15:16
  • Regex is too complicated. It's really just the worst. The learning curve is high *just* to get started, and even far higher to actually be good at it. So most people just go to the internet to copy/paste well tested Regex expressions. And even THEN they often aren't perfect. Try looking for the perfect Regex for email and you'll see what I mean. It has its uses, particularly in declarative contexts such as IIS Rewrite Rules where you can't use procedural logic. But I have absolutely zero use for it in c# where I can write perfectly readable and maintainable code. – BVernon Apr 07 '22 at 07:17
145
var search = "world";
var replacement = "csharp";
string result = Regex.Replace(
    stringToLookInto,
    Regex.Escape(search), 
    replacement.Replace("$","$$"), 
    RegexOptions.IgnoreCase
);

The Regex.Escape is useful if you rely on user input which can contains Regex language elements

Update

Thanks to comments, you actually don't have to escape the replacement string.

Here is a small fiddle that tests the code:

using System;
using System.Text.RegularExpressions;           
public class Program
{
    public static void Main()
    {

        var tests = new[] {
            new { Input="abcdef", Search="abc", Replacement="xyz", Expected="xyzdef" },
            new { Input="ABCdef", Search="abc", Replacement="xyz", Expected="xyzdef" },
            new { Input="A*BCdef", Search="a*bc", Replacement="xyz", Expected="xyzdef" },
            new { Input="abcdef", Search="abc", Replacement="x*yz", Expected="x*yzdef" },       
            new { Input="abcdef", Search="abc", Replacement="$", Expected="$def" },
        };


        foreach(var test in tests){
            var result = ReplaceCaseInsensitive(test.Input, test.Search, test.Replacement);

            Console.WriteLine(
                "Success: {0}, Actual: {1}, {2}",
                result == test.Expected,
                result,
                test
            );

        }


    }

    private static string ReplaceCaseInsensitive(string input, string search, string replacement){
        string result = Regex.Replace(
            input,
            Regex.Escape(search), 
            replacement.Replace("$","$$"), 
            RegexOptions.IgnoreCase
        );
        return result;
    }
}

Its output is:

Success: True, Actual: xyzdef, { Input = abcdef, Search = abc, Replacement = xyz, Expected = xyzdef } 
Success: True, Actual: xyzdef, { Input = ABCdef, Search = abc, Replacement = xyz, Expected = xyzdef }
Success: True, Actual: xyzdef, { Input = A*BCdef, Search = a*bc, Replacement = xyz, Expected = xyzdef } 
Success: True, Actual: x*yzdef, { Input = abcdef, Search = abc, Replacement = x*yz, Expected = x*yzdef} 
Success: True, Actual: $def, { Input = abcdef, Search = abc, Replacement = $, Expected = $def }
Steve B
  • 36,818
  • 21
  • 101
  • 174
  • 2
    This method fails if replacement = "!@#$%^&*()" You get "!@\#\$%\^&\*\(\)" replaced instead. – Kcoder Oct 10 '14 at 22:45
  • 2
    The second `Regex.Escape` is bad, it will prefix special characters with backslashes. Seems like the best way is .Replace("$", "$$"), which is kinda dumb (http://stackoverflow.com/a/10078353). – Danny Tuppeny Apr 17 '15 at 08:43
  • 2
    @dannyTuppeny: you are right... I updated the answer accordingly – Steve B Apr 17 '15 at 08:53
106

2.5X FASTER and MOST EFFECTIVE method than other's regular expressions methods:

/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with another 
/// specified string according the type of search to use for the specified string.
/// </summary>
/// <param name="str">The string performing the replace method.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string replace all occurrences of <paramref name="oldValue"/>. 
/// If value is equal to <c>null</c>, than all occurrences of <paramref name="oldValue"/> will be removed from the <paramref name="str"/>.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param>
/// <returns>A string that is equivalent to the current string except that all instances of <paramref name="oldValue"/> are replaced with <paramref name="newValue"/>. 
/// If <paramref name="oldValue"/> is not found in the current instance, the method returns the current instance unchanged.</returns>
[DebuggerStepThrough]
public static string Replace(this string str,
    string oldValue, string newValue,
    StringComparison comparisonType)
{

    // Check inputs.
    if (str == null)
    {
        // Same as original .NET C# string.Replace behavior.
        throw new ArgumentNullException(nameof(str));
    }
    if (oldValue == null)
    {
        // Same as original .NET C# string.Replace behavior.
        throw new ArgumentNullException(nameof(oldValue));
    }
    if (oldValue.Length == 0)
    {
        // Same as original .NET C# string.Replace behavior.
        throw new ArgumentException("String cannot be of zero length.");
    }
    if (str.Length == 0)
    {
        // Same as original .NET C# string.Replace behavior.
        return str;
    }
    

    //if (oldValue.Equals(newValue, comparisonType))
    //{
    //This condition has no sense
    //It will prevent method from replacesing: "Example", "ExAmPlE", "EXAMPLE" to "example"
    //return str;
    //}



    // Prepare string builder for storing the processed string.
    // Note: StringBuilder has a better performance than String by 30-40%.
    StringBuilder resultStringBuilder = new StringBuilder(str.Length);



    // Analyze the replacement: replace or remove.
    bool isReplacementNullOrEmpty = string.IsNullOrEmpty(newValue);



    // Replace all values.
    const int valueNotFound = -1;
    int foundAt;
    int startSearchFromIndex = 0;
    while ((foundAt = str.IndexOf(oldValue, startSearchFromIndex, comparisonType)) != valueNotFound)
    {
        
        // Append all characters until the found replacement.
        int charsUntilReplacment = foundAt - startSearchFromIndex;
        bool isNothingToAppend = charsUntilReplacment == 0;
        if (!isNothingToAppend)
        {
            resultStringBuilder.Append(str, startSearchFromIndex, charsUntilReplacment);
        }
        


        // Process the replacement.
        if (!isReplacementNullOrEmpty)
        {
            resultStringBuilder.Append(newValue);
        }
        

        // Prepare start index for the next search.
        // This needed to prevent infinite loop, otherwise method always start search 
        // from the start of the string. For example: if an oldValue == "EXAMPLE", newValue == "example"
        // and comparisonType == "any ignore case" will conquer to replacing:
        // "EXAMPLE" to "example" to "example" to "example" … infinite loop.
        startSearchFromIndex = foundAt + oldValue.Length;
        if (startSearchFromIndex == str.Length)
        {
            // It is end of the input string: no more space for the next search.
            // The input string ends with a value that has already been replaced. 
            // Therefore, the string builder with the result is complete and no further action is required.
            return resultStringBuilder.ToString();
        }
    }
    

    // Append the last part to the result.
    int charsUntilStringEnd = str.Length - startSearchFromIndex;
    resultStringBuilder.Append(str, startSearchFromIndex, charsUntilStringEnd);


    return resultStringBuilder.ToString();

}

Note: ignore case == StringComparison.OrdinalIgnoreCase as parameter for StringComparison comparisonType. It is the fastest, case-insensitive way to replace all values.


Advantages of this method:

  • High CPU and MEMORY efficiency;
  • It is the fastest solution, 2.5 times faster than other's methods with regular expressions (proof in the end);
  • Suitable for removing parts from the input string (set newValue to null), optimized for this;
  • Same as original .NET C# string.Replace behavior, same exceptions;
  • Well commented, easy to understand;
  • Simpler – no regular expressions. Regular expressions are always slower because of their versatility (even compiled);
  • This method is well tested and there are no hidden flaws like infinite loop in other's solutions, even highly rated:

@AsValeO: Not works with Regex language elements, so it's not universal method

@Mike Stillion: There is a problem with this code. If the text in new is a superset of the text in old, this can produce an endless loop.


Benchmark-proof: this solution is 2.59X times faster than regex from @Steve B., code:

// Results:
// 1/2. Regular expression solution: 4486 milliseconds
// 2/2. Current solution: 1727 milliseconds — 2.59X times FASTER! than regex!

// Notes: the test was started 5 times, the result is an average; release build.

const int benchmarkIterations = 1000000;
const string sourceString = "aaaaddsdsdsdsdsd";
const string oldValue = "D";
const string newValue = "Fod";
long totalLenght = 0;

Stopwatch regexStopwatch = Stopwatch.StartNew();
string tempString1;
for (int i = 0; i < benchmarkIterations; i++)
{
    tempString1 = sourceString;
    tempString1 = ReplaceCaseInsensitive(tempString1, oldValue, newValue);

    totalLenght = totalLenght + tempString1.Length;
}
regexStopwatch.Stop();



Stopwatch currentSolutionStopwatch = Stopwatch.StartNew();
string tempString2;
for (int i = 0; i < benchmarkIterations; i++)
{
    tempString2 = sourceString;
    tempString2 = tempString2.Replace(oldValue, newValue,
        StringComparison.OrdinalIgnoreCase);

    totalLenght = totalLenght + tempString2.Length;
}
currentSolutionStopwatch.Stop();

Original idea – @Darky711; thanks @MinerR for StringBuilder.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
Oleg Zarevennyi
  • 2,753
  • 1
  • 21
  • 21
  • 6
    I bet you can make this even faster using a StringBuilder rather than a string. – MineR Mar 01 '18 at 06:21
  • 2
    @MineR You are right, I originally just updated the @Darky711 solution without infinite loop, so I used the `String`. However, the `StringBuilder` is really faster by **30-40%** than the `String`. I've updated the solution. Thanks ;) – Oleg Zarevennyi Mar 01 '18 at 20:06
  • 2
    Interesting approach. Probably the better one (better than mine :)) when performance matters. Typically a method to add to a common shared code library. – Steve B Nov 05 '18 at 08:30
  • 2
    The use of 'nameof' expressions makes this valid only for C# 6.0 and beyond. If you're in VS2013, you can use it by simply deleting the operands in the exceptions. – LanchPad Mar 21 '19 at 16:06
  • For the commented out "//if (oldValue.Equals(newValue, comparisonType))" replace comparisonType with StringComparison.Ordinal? – Roger Willcocks May 27 '20 at 00:10
  • 1
    You have a bug when calling replace("abc","abc\u00AD","def",StringComparison.CurrentCulture) the expected result is "def" (\u00AD is a soft hyphen - test case taken from .net core string replace test cases at https://github.com/dotnet/runtime/blob/1bfdab43807670a7fba468d09b9afd1a72d1f8e3/src/libraries/System.Runtime/tests/System/StringTests.cs ). The fix is to change "if (startSearchFromIndex == str.Length)" to "if (startSearchFromIndex >= str.Length)" – Dani Avni Jan 15 '21 at 07:38
  • It doesn't yield the same result. When done with ignore case this method replaces the lower cased string while the method of @Darin Dimitrov replaces the original cased string. – Orhan Nov 07 '22 at 15:30
37

Lots of suggestions using Regex. How about this extension method without it:

public static string Replace(this string str, string old, string @new, StringComparison comparison)
{
    @new = @new ?? "";
    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(old) || old.Equals(@new, comparison))
        return str;
    int foundAt = 0;
    while ((foundAt = str.IndexOf(old, foundAt, comparison)) != -1)
    {
        str = str.Remove(foundAt, old.Length).Insert(foundAt, @new);
        foundAt += @new.Length;
    }
    return str;
}
DATEx2
  • 3,585
  • 1
  • 24
  • 26
Oleg Fridman
  • 1,560
  • 16
  • 16
  • Note that the comparison argument is not being used to do the actual replacement (it is always case insensitive) – Bolo Mar 30 '16 at 18:52
  • 3
    There is a problem with this code. If the text in **new** is a superset of the text in **old**, this can produce an endless loop. Once **new** is inserted at **FoundAt**, the value of **FoundAt** needs to be advanced by the length of **new**. – Mike Stillion Sep 20 '16 at 13:34
  • `comparison` parameter should be used in `IndexOf`, instead of `StringComparison.CurrentCultureIgnoreCase` – Maxence Dec 08 '16 at 17:18
  • @Bolo I've edited it to use the comparison argument (might take a bit to be peer reviewed). – bradlis7 Jan 08 '18 at 19:52
  • I fixed the **infinite loop** problem of this method. Increase speed by **30-40%** by replacing `String` with `StringBuilder`. And add missed comments. Please check - https://stackoverflow.com/a/45756981/1998097 – Oleg Zarevennyi Mar 01 '18 at 20:11
  • 2
    I'd also separate this condition for returning the new string: `if(old.Equals(@new, comparison)) return @new;`, since the new string may differ in uppercase/lowercase. – sɐunıɔןɐqɐp Jun 25 '18 at 11:06
  • honestly, @new hurts my eyes – Selman Genç Dec 20 '19 at 12:59
34

Extensions make our lives easier:

static public class StringExtensions
{
    static public string ReplaceInsensitive(this string str, string from, string to)
    {
        str = Regex.Replace(str, from, to, RegexOptions.IgnoreCase);
        return str;
    }
}
Petrucio
  • 5,491
  • 1
  • 34
  • 33
  • 16
    And escaping makes our lives less buggy :-) return Regex.Replace(input, Regex.Escape(search), replacement.Replace("$", "$$"), RegexOptions.IgnoreCase); – Vman Dec 13 '17 at 12:38
19

You can use the Microsoft.VisualBasic namespace to find this helper function:

Replace(sourceString, "replacethis", "withthis", , , CompareMethod.Text)
John Smith
  • 7,243
  • 6
  • 49
  • 61
user2991288
  • 361
  • 2
  • 5
  • 2
    I was proud of my answer until I saw this which is a better answer because it is built in. Ex: Strings.Replace("TeStInG123", "t", "z", 1, -1, CompareMethod.Text) returns "zeSzInG123" – Bolo Nov 10 '17 at 16:31
  • 2
    Warning, Strings.Replace returns null if the string being search is an empty string. – Mafu Josh Sep 17 '18 at 14:02
  • 2
    In .Net 4.7.2, you need to add a reference to Microsoft.VisualBasic to get this to work. In .Net Core, the Microsoft.VisualBasic.Strings class (in Version 10.3.0 anyway) does not appear to implement the Replace function. This works in Powershell as well if you Add-Class -AssemblyName Microsoft.VisualBasic first. – Prof Von Lemongargle Nov 29 '18 at 15:29
  • I don't like this solution because it makes a copy of the entire string even if nothing is replaced, so it can be very bulky. I rewrote it to not do that a while back, since strings are immutable. – Brain2000 Jun 14 '23 at 23:18
19

.Net Core has this method built-in: Replace(String, String, StringComparison) Doc. Now we can simply write: "...".Replace("oldValue", "newValue", StringComparison.OrdinalIgnoreCase)

ZZY
  • 3,689
  • 19
  • 22
8

Modified @Darky711's answer to use the passed in comparison type and match the framework replace naming and xml comments as closely as possible.

/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
/// </summary>
/// <param name="str">The string performing the replace method.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string replace all occurrances of oldValue.</param>
/// <param name="comparisonType">Type of the comparison.</param>
/// <returns></returns>
public static string Replace(this string str, string oldValue, string @newValue, StringComparison comparisonType)
{
    @newValue = @newValue ?? string.Empty;
    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(oldValue) || oldValue.Equals(@newValue, comparisonType))
    {
        return str;
    }
    int foundAt;
    while ((foundAt = str.IndexOf(oldValue, 0, comparisonType)) != -1)
    {
        str = str.Remove(foundAt, oldValue.Length).Insert(foundAt, @newValue);
    }
    return str;
}
Bolo
  • 1,494
  • 1
  • 19
  • 19
6

(Edited: wasn't aware of the `naked link' problem, sorry about that)

Taken from here:

string myString = "find Me and replace ME";
string strReplace = "me";
myString = Regex.Replace(myString, "me", strReplace, RegexOptions.IgnoreCase);

Seems you are not the first to complain of the lack of case insensitive string.Replace.

Nick
  • 5,765
  • 5
  • 27
  • 36
3

Extending Petrucio's answer with Regex.Escape on the search string, and escaping matched group as suggested in Steve B's answer (and some minor changes to my taste):

public static class StringExtensions
{
    public static string ReplaceIgnoreCase(this string str, string from, string to)
    {
        return Regex.Replace(str, Regex.Escape(from), to.Replace("$", "$$"), RegexOptions.IgnoreCase);
    }
}

Which will produce the following expected results:

Console.WriteLine("(heLLo) wOrld".ReplaceIgnoreCase("(hello) world", "Hi $1 Universe")); // Hi $1 Universe
Console.WriteLine("heLLo wOrld".ReplaceIgnoreCase("(hello) world", "Hi $1 Universe"));   // heLLo wOrld

However without performing the escapes you would get the following, which is not an expected behaviour from a String.Replace that is just case-insensitive:

Console.WriteLine("(heLLo) wOrld".ReplaceIgnoreCase_NoEscaping("(hello) world", "Hi $1 Universe")); // (heLLo) wOrld
Console.WriteLine("heLLo wOrld".ReplaceIgnoreCase_NoEscaping("(hello) world", "Hi $1 Universe"));   // Hi heLLo Universe
Community
  • 1
  • 1
Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
3

I have wrote extension method:

public static string ReplaceIgnoreCase(this string source, string oldVale, string newVale)
    {
        if (source.IsNullOrEmpty() || oldVale.IsNullOrEmpty())
            return source;

        var stringBuilder = new StringBuilder();
        string result = source;

        int index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);

        while (index >= 0)
        {
            if (index > 0)
                stringBuilder.Append(result.Substring(0, index));

            if (newVale.IsNullOrEmpty().IsNot())
                stringBuilder.Append(newVale);

            stringBuilder.Append(result.Substring(index + oldVale.Length));

            result = stringBuilder.ToString();

            index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
        }

        return result;
    }

I use two additional extension methods for previous extension method:

    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }

    public static bool IsNot(this bool val)
    {
        return val == false;
    }
Georgy Batalov
  • 469
  • 4
  • 6
3

Doesn't this work: I cant imaging anything else being much quicker or easier.

public static class ExtensionMethodsString
{
    public static string Replace(this String thisString, string oldValue, string newValue, StringComparison stringComparison)
    {
        string working = thisString;
        int index = working.IndexOf(oldValue, stringComparison);
        while (index != -1)
        {
            working = working.Remove(index, oldValue.Length);
            working = working.Insert(index, newValue);
            index = index + newValue.Length;
            index = working.IndexOf(oldValue, index, stringComparison);
        }
        return working;
    }
}
Tom Robson
  • 31
  • 1
  • 2
  • I don't know if it's faster but it's concise, doesn't use regex overhead and potential problems and uses the built-in StringComparison. – fvlinden Jun 14 '20 at 08:12
2

Another way is to ignore the case sensitivity in String.Replace() using the option StringComparison.CurrentCultureIgnoreCase

string.Replace("World", "csharp", StringComparison.CurrentCultureIgnoreCase)
Gama Sharma
  • 51
  • 1
  • 7
  • This is entirely valid, *but* requires modern frameworks - that *we should already be using*; I'm guessing the downvote was from a .NET Framework user (it exists in .NET Core 2 and later, and .NET Standard 2.1) - or it could be an observation that it duplicates an existing answer: https://stackoverflow.com/a/64677285/23354 – Marc Gravell Aug 10 '22 at 09:24
1

Using @Georgy Batalov solution I had a problem when using the following example

string original = "blah,DC=bleh,DC=blih,DC=bloh,DC=com"; string replaced = original.ReplaceIgnoreCase(",DC=", ".")

Below is how I rewrote his extension

public static string ReplaceIgnoreCase(this string source, string oldVale, 
string newVale)
    {
        if (source.IsNullOrEmpty() || oldVale.IsNullOrEmpty())
            return source;

        var stringBuilder = new StringBuilder();
        string result = source;

        int index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
        bool initialRun = true;

        while (index >= 0)
        {
            string substr = result.Substring(0, index);
            substr = substr + newVale;
            result = result.Remove(0, index);
            result = result.Remove(0, oldVale.Length);

            stringBuilder.Append(substr);

            index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
        }

        if (result.Length > 0)
        {
            stringBuilder.Append(result);
        }

        return stringBuilder.ToString();
    }
Bbb
  • 517
  • 6
  • 27
1

My this Method could Ignore Case as well as Select Only Whole Word

public static string Replace(this string s, string word, string by, StringComparison stringComparison, bool WholeWord)
{
    s = s + " ";
    int wordSt;
    StringBuilder sb = new StringBuilder();
    while (s.IndexOf(word, stringComparison) > -1)
    {
        wordSt = s.IndexOf(word, stringComparison);
        if (!WholeWord || ((wordSt == 0 || !Char.IsLetterOrDigit(char.Parse(s.Substring(wordSt - 1, 1)))) && !Char.IsLetterOrDigit(char.Parse(s.Substring(wordSt + word.Length, 1)))))
        {
            sb.Append(s.Substring(0, wordSt) + by);
        }
        else
        {
            sb.Append(s.Substring(0, wordSt + word.Length));
        }
        s = s.Substring(wordSt + word.Length);
    }
    sb.Append(s);
    return sb.ToString().Substring(0, sb.Length - 1);
}
Honey22Sharp
  • 168
  • 1
  • 6
1

I recommend the StringComparison.CurrentCultureIgnoreCase method as proposed by ZZY / Gama Sharma. This is just another technique that could be used with LINQ:

List<string> ItemsToRedact = new List<string> { "star", "citizen", "test", "universe"};

string Message = "Just like each sTaR is unique yet mAkes the uniVERSE what it is, the light in you makes you who you are";

List<string> ReplacementList = Message.Split(' ').Where(x => ItemsToRedact.Contains(x.ToLower())).ToList();

foreach (var word in ReplacementList)
{
     Message = Message.Replace(word, "[Redacted] ");
}

Console.WriteLine(Message);

returns: Just like each [Redacted] is unique yet mAkes the [Redacted] what it is, the light in you makes you who you are

This code could be further distilled but I broke it up for readability

Kowalchick
  • 428
  • 3
  • 13
  • This solution has at least two bugs. 1 - redacted words would contain an extra trailing space because this solution replaces a word (without a trailing space) with "[Redacted] " (with a trailing space). 2 - words containing part of a matched word would be replaced with "[Redacted] ". For example, if the Message was "sTaRt the sTaR forge" and "star" was being redacted, then the result would be "s[Redacted] the [Redacted] forge". – jhenninger Mar 13 '23 at 15:17
0

Below function is to remove all match word like (this) from the string set. By Ravikant Sonare.

private static void myfun()
{
    string mystring = "thiTHISThiss This THIS THis tThishiThiss. Box";
    var regex = new Regex("this", RegexOptions.IgnoreCase);
    mystring = regex.Replace(mystring, "");
    string[] str = mystring.Split(' ');
    for (int i = 0; i < str.Length; i++)
    {
        if (regex.IsMatch(str[i].ToString()))
        {
            mystring = mystring.Replace(str[i].ToString(), string.Empty);

        }
    }
    Console.WriteLine(mystring);
}
0

You can also try the Regex class.

var regex = new Regex( "camel", RegexOptions.IgnoreCase ); var newSentence = regex.Replace( sentence, "horse" );

sud007
  • 5,824
  • 4
  • 56
  • 63
Hiren Patel
  • 317
  • 4
  • 7
0

Use this, Tested and 100% Worked!

For VB.NET

Dim myString As String
Dim oldValue As String
Dim newValue As String

myString = Form1.TextBox1.Text
oldValue = TextBox1.Text
newValue = TextBox2.Text

Dim working As String = myString
Dim index As Integer = working.IndexOf(oldValue, StringComparison.CurrentCultureIgnoreCase)

While index <> -1
    working = working.Remove(index, oldValue.Length)
    working = working.Insert(index, newValue)
    index = index + newValue.Length
    index = working.IndexOf(oldValue, index, StringComparison.CurrentCultureIgnoreCase)
    Form1.TextBox1.Text = working
End While

For C#

private void Button2_Click(System.Object sender, System.EventArgs e)
{
    string myString;
    string oldValue;
    string newValue;

    myString = Form1.TextBox1.Text;
    oldValue = TextBox1.Text;
    newValue = TextBox2.Text;

    string working = myString;
    int index = working.IndexOf(oldValue, StringComparison.CurrentCultureIgnoreCase);

    while (index != -1)
    {
        working = working.Remove(index, oldValue.Length);
        working = working.Insert(index, newValue);
        index = index + newValue.Length;
        index = working.IndexOf(oldValue, index, StringComparison.CurrentCultureIgnoreCase);
        Form1.TextBox1.Text = working;
    }
}
Fabio Mendes Soares
  • 1,357
  • 5
  • 20
  • 30
Hedi
  • 21
  • 5
-1

I prefer this - "Hello World".ToLower().Replace( "world", "csharp" );

Harshal
  • 105
  • 4
  • 1
    This will lowercase everything, even words that weren't supposed to be replaced. – JJJ Jan 18 '19 at 08:47
  • Obviously, you can use this only if you are not bothered about the case. – Harshal Jan 21 '19 at 06:59
  • I believe this is the best answer for simple use of the Replace method in most cases, despite all of the down votes. If you are doing an edge case that is more complex, then you probably should not be using Replace() to begin with. – Chris M. Jun 01 '21 at 15:45