To replace characters or substrings in a string, use the Replace method. For instance, to remove display formatting from a phone number such as (919) 555-1212, the following code can be used:
string phoneNumber = "(919) 555-1212"; string fixedPhoneNumber =
phoneNumber.Replace( "(", "" ). Replace( ")", "" )
.Replace( "-", "" ) .Replace( " ", "" ); Console.WriteLine( fixedPhoneNumber );
Notice how the Replace method is used. Each time Replace is called, a new string is created. Thus, the cascading use of the Replace method to remove all unwanted strings is necessary.