Recipe2.10.Removing or Replacing Characters Within a String


Recipe 2.10. Removing or Replacing Characters Within a String

Problem

You have some text within a string that needs to be either removed or replaced with a different character or string. Since the replacing operation is somewhat simple, using a regular expression to aid in the replacing operation is not worth the overhead.

Solution

To remove a substring from a string, use the Remove instance method on the String class. For example:

 string name = "Doe, John"; name = name.Remove(3, 1); Console.WriteLine(name); 

This code creates a new string and then sets the name variable to refer to it. The string contained in name now looks like this:

 Doe John 

If performance is critical, and particularly if the string removal operation occurs in a loop so that the operation is performed multiple times, you can instead use the Remove method of the StringBuilder object. The following code modifies the str variable so that its value becomes 12345678:

 StringBuilder str = new StringBuilder("1234abc5678", 12); str.Remove(4, 3); Console.WriteLine(str); 

To replace a delimiting character within a string, use the following code:

 string commaDelimitedString = "100,200,300,400,500"; commaDelimitedString = commaDelimitedString.Replace(',', ':'); Console.WriteLine(commaDelimitedString); 

This code creates a new string and then makes the commaDelimitedString variable refer to it. The string in commaDelimitedString now looks like this:

 100:200:300:400:500 

To replace a placeholding string within a string, use the following code:

 string theName = "Mary"; string theObject = "car"; string ID = "This <ObjectPlaceholder> is the property of <NamePlaceholder>."; ID = ID.Replace("<ObjectPlaceholder>", theObject); ID = ID.Replace("<NamePlaceholder>", theName); Console.WriteLine(ID); 

This code creates a new string and then makes the ID variable refer to it. The string in ID now looks like this:

 This car is the property of Mary. 

As when removing a portion of a string, you may, for performance reasons, choose to use the Replace method of the StringBuilder class instead. For example:

 string newName = "John Doe"; str = new StringBuilder("name = <NAME>"); str.Replace("<NAME>", newName); Console.WriteLine(str.ToString( )); str.Replace('=', ':'); Console.WriteLine(str.ToString( )); str = new StringBuilder("name1 = <FIRSTNAME>, name2 = <FIRSTNAME>"); str.Replace("<FIRSTNAME>", newName, 7, 12); Console.WriteLine(str.ToString( )); str.Replace('=', ':', 0, 7); Console.WriteLine(str.ToString( )); 

This code produces the following results:

 name = John Doe name : John Doe name1 = John Doe, name2 = <FIRSTNAME> name1 : John Doe, name2 = <FIRSTNAME> 

Note that when using the StringBuilder class, you must use the System.Text namespace.

Discussion

The String class provides two methods that allow easy removal and modification of characters in a string: the Remove instance method and the Replace instance method. The Remove method deletes a specified number of characters starting at a given location within a string. This method returns a new string object containing the modified string.

The Replace instance method that the String class provides is very useful for removing characters from a string and replacing them with a new character or string. At any point where the Replace method finds an instance of the string passed in as the first parameter, it will replace it with the string passed in as the second parameter. The Replace method is case-sensitive and returns a new string object containing the modified string. If the string being searched for cannot be found in the original string, the method returns a reference to the original string object.

The Replace and Remove methods on a string object always create a new string object that contains the modified text. If this action hurts performance, consider using the Replace and Remove methods on the StringBuilder class.

The Remove method of the StringBuilder class is not overloaded and is straightforward to use. Simply give it a starting position and the number of characters to remove. This method returns a reference to the same instance of the StringBuilder object with the Replace method that modified the string value.

The Replace method of the StringBuilder class allows for fast character or string replacement to be performed on the original StringBuilder object. These methods return a reference to the same instance of the StringBuilder object with the Replace method that was called. If you are performing a replace operation that uses a format string under your control, then you should use the AppendFormat method of the StringBuilder class.

Note that this method is case-sensitive.

See Also

See the "String.Replace Method," "String.Remove Method," "StringBuilder.Replace Method," and "StringBuilder.Remove Method" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net