To replace one substring with another in a string in C#, use the method Replace()
. There are two use cases:
Replace(char old, char new) -
char old
replaced with char new
;
Replace(string old, string new) - string old
is replaced by string new
, i.e. more than one character can be replaced.
Inline replacement example in C#:
string phone = "+1-234-567-89-10";
// hyphens are changed to spaces
string edited_phone = phone.Replace("-", " ");
// +1 234 567 89 10