C# || How To Replace Entire Words In A String Using C#
The following is a module with functions which demonstrates how to replace entire words in a string using C#.
This function is different from String.Replace in that instead of replacing all substring instances in a word, it replaces all instances of matching entire words in a string.
1. Replace Entire Word
The example below demonstrates the use of ‘Utils.Extensions.ReplaceWord‘ to replace an entire word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Replace Entire Word using Utils; var word = "This docment uses 3 other docments to docment the docmentation"; // Replace the entire word in the string var result = word.ReplaceWord("docment", "document"); Console.WriteLine(result); // expected output: /* This document uses 3 other docments to document the docmentation */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 12, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; namespace Utils { public static class Extensions { /// <summary> /// Replaces the entire word in the string /// </summary> /// <param name="source">The source string</param> /// <param name="wordToReplace">The word to replace</param> /// <param name="replacementWord">The replacement word</param> /// <param name="regexOptions">The search options</param> /// <returns>The modified source string</returns> public static string ReplaceWord(this string source, string wordToReplace, string replacementWord , System.Text.RegularExpressions.RegexOptions regexOptions = System.Text.RegularExpressions.RegexOptions.None) { var pattern = $@"\b{System.Text.RegularExpressions.Regex.Escape(wordToReplace)}\b"; var result = System.Text.RegularExpressions.Regex.Replace(source, pattern, replacementWord, regexOptions); return result; } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 12, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using Utils; public class Program { static void Main(string[] args) { try { var word = "This docment uses 3 other docments to docment the docmentation"; // Replace the entire word in the string var result = word.ReplaceWord("docment", "document"); Display(result); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply