I have a string called \"hello world\"
I need to replace the word \"world\" to \"csharp\"
for this I use:
string.Replace(\"World\", \"csharp\
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;
}