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. For example:
string name = "Lau, Andy";
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:
Lau Andy
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);
On the other hand, 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 copy of the original string object. For example the snippet code below:
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 place-holding string within a string, see the following code:
string theName = "Jacky"; string theObject = "car"; string ID = "Thisis the property of ."; ID = ID.Replace(" ", theObject); ID = ID.Replace(" ", 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 Jacky.
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 = "David Beckham"; str = new StringBuilder("name ="); str.Replace(" ", newName); Console.WriteLine(str.ToString()); str.Replace(''='', '':''); Console.WriteLine(str.ToString()); str = new StringBuilder("name1 = , name2 = "); str.Replace(" ", newName, 7, 12); Console.WriteLine(str.ToString( )); str.Replace(''='', '':'', 0, 7); Console.WriteLine(str.ToString());
This code produces the following results:
name = David Beckham name : David Beckham name1 = David Beckham, name2 = <FIRSTNAME> name1 : David Beckham, name2 = <FIRSTNAME>
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 straight-foward 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 whose Replace method 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 whose Replace method was called. Just remember that this method is case-sensitive.
Popularity: 3% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply