Home > Blockchain >  C# - String replace with Ignore Case as Well as Whole Word Only in .Net Framework without Regex
C# - String replace with Ignore Case as Well as Whole Word Only in .Net Framework without Regex

Time:11-13

Regex is not replacing special characters properly. So any alternative or codes to fix this method.

Until now I was using this method from regex to replace

public static string Replace(this string s,string word ,string by ,bool IgnoreCase,bool WholeWord)
    {
        RegexOptions regexOptions = RegexOptions.None;
        if (IgnoreCase) regexOptions = RegexOptions.IgnoreCase;
        if (WholeWord) word = @"\b"   Regex.Escape(word)   @"\b";
        return Regex.Replace(s, word, by, regexOptions);
    }

I have a string

string str = "Apple , Mango , Banana.";

if I replace

str = str.Replace("apple", "Apples", true, true);

Results

Apples, Mango, Banana.

It works fine with any letter(s) or digit(s), but it didn't work on non-letter-digit like comma(,), dot(.) and others @,#,$,",:

Example

str = str.Replace(",", " and ", true, true); 

It didn't do anything.

Another Example I have string "She is Mari and she is Marijane."; if I want to Replace Mari to Mira Normal Replace will Replace both Mari and Marijane Some places Mari is at beginnig and some places at end connected with fullstop(.) or sometimes used with commas.

Note: I need both IgnoreCase and WholeWord as bool

There are some examples already, but none of them can combine (IgnoreCase and WholeWord), and I need in .Net Framework 4.8 (It uses C# version 7.3 or lower)

So please somebody could Help me in this situation

Thanks in advance and sorry for my bad English

CodePudding user response:

I can Answer to that Question for older version of .NET Frameworks without Using So Called Regex or Third-Party Package

Just use These Codes.

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

If you want StringComparison as bool then Add this

public static string Replace(this string s, string word, string by, bool IgnoreCase, bool WholeWord)
    {
        StringComparison stringComparison = StringComparison.Ordinal;
        if (IgnoreCase) stringComparison = StringComparison.OrdinalIgnoreCase;
        return s.Replace(word, by, stringComparison, WholeWord);
    }

You Can Either Keep Both or Merge them. It's all up to you.

  • Related