by
1
9
7,267
16
Top 1% !
Popular
Famous
Pearl of Wisdom
Tagged
Easy-to-find
Specified
Refined
OpenSource
Popularity: 911st place

Published on:

.NETFramework4.0
Architecturex86_64
Extension.cs
InputName0stringToSearch
InputName1find
InputName2replace
InputType0string
InputType1string
InputType2string
LanguageCSharp
LicenseMIT_X11
MethodNameReplaceExactWord
OSWindows
OutputTypestring

C#: Replace an exact word in a sentence

Same as String.Replace but with case-insensitive matches and search for the exact word.
E.g. searching in "Java and Javascript with Java." the word 'Java', and replacing it with 'Python' results in "Python and Javascript with Python."
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/25660/C---Replace-an-exact-word-in-a-sentence?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static string ReplaceExactWord(this string stringToSearch, string find, string replace) { string pattern = @"\b" + find + @"\b"; return Regex.Replace(stringToSearch, pattern, replace, RegexOptions.IgnoreCase); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus