Remove and Replace in C# .NET

|

The Remove Method

As its name suggests, this method is used to Remove characters from a string of text. Here's a simple example of its use:
string oldString = "some text text text";
MessageBox.Show(oldString);
string newString = oldString.Remove(10, 9);
MessageBox.Show(newString);
Remove takes two parameters. The first one is what position in your string you want to start at. (The count starts at zero.) The second parameter is how many characters you want to delete, starting from the position you specified. Add another button to your form and try out the code above.

The Replace Method

The Replace method, you won't be surprised to hear, can be used to replace characters in your string. Here's an example of its use:
string spellingError = "mistak";
spellingError = spellingError.Replace(spellingError, "mistake");
The Replace method takes two parameters, the old word and the new word. In the code above, we're replacing "mistak" with "mistake".

0 comments:

Post a Comment