When operating on strings, we often need to delete or replace some substrings. Remove () and Replace () functions are useful in this case.
Remove ()-delete a part of the substring
We all know that substring can return part of a string. When we want to retain a part of the string substring to discard other parts, we can use substring. When we want to delete a part of the string and retain other parts, we will use Remove.
Remove has two forms:
- Remove (int startIndex)
- Delete all characters in the string from the specified position to the last position.
- Remove (int startIndex, int length)
- Delete a specified number of characters from the specified position in the instance.
Remove checks the region,
For the first form when
1. startIndex is smaller than zero or
2. the position specified by startIndex is not in this string;
For the second form when
1. startIndex or count is smaller than zero or
2. The sum of startIndex and count specifies the location of the real exception.
An exception is thrown.
In Remove () and substring (), it is easy to select which one. See the following example:
String test = "Now is the time for all good men to come to the aid of their country .";
// Take the first 10 Characters
Var sliceUsingSubstring = test. Substring (0, 10 );
// Delete All characters after 10th characters
Var sliceUsingRemove = test. Remove (10 );
If we want to get the first 10 characters, both methods can be used, but Remove seems a little concise, because you do not need to point the starting point.
So what if you want to get the last 10 characters?
String test = "Now is the time for all good men to come to the aid of their country .";
// Obtain the part from length-10 to the end.
Var sliceUsingSubstring = test. Substring (test. Length-10 );
// Delete from the beginning to length-10, and leave the rest
Var sliceUsingRemove = test. Remove (0, test. Length-10 );
In this case, the substring looks more concise.
Now it seems that they are actually, but. net provides two methods in this case, which can be used based on your preferences.
However, when we want to retain or delete the middle part of the string, we can see that they are different:
// Obtain a substring of 10 starting from 10th
Var sliceUsingSubstring = test. Substring (10, 10 );
// Delete the part after 20th characters, and then delete the first 10
Var sliceUsingRemove = test. Remove (20). Remove (0, 10 );
In this case, it is obvious that substring is easy to read and only needs one operation.
However, if we want to delete a string in the middle:
// Obtain the substring starting from 0 and ending from 20th.
Var sliceUsingSubstring = test. Substring (0, 10) + test. Substring (20 );
// Delete 10 characters from 10th
Var sliceUsingRemove = test. Remove (10, 10 );
In this case, remove is much simpler and easier to read.
Replace ()-Replace char or String
Replace all matches of the specified Unicode character or String in this instance with other specified Unicode characters orString.
Two reloads
Replace (string oldValue, string newValue)
SetStringReplace all matching items with other specifiedString.
String test = "Now is the time for all good men to come to the aid of their country .";
Var politicallyCorrect = test. Replace ("men", "people ");
Var spacesToPipes = test. Replace ('', '| ');
Var withoutThe = test. Replace ("the", string. Empty );
In the next example, you want to change a piece of data containing the "<BR/>" HTML code to Environment. NewLine:
string test = "Some data & markup was loaded from a data source.<BR/> Oh look, we started a new line!";
var cleansedData = test.Replace("<BR/>", Environment.NewLine);
var moreCleansedData = test.Replace("&", "&")
.Replace(" ", " ")
.Replace("<BR/>", Environment.NewLine);
At the same time, I conducted the same experiment on stringbuilder and found that in this case, stringbuilder runs very slowly:
var morePerformantCleansedData = new StringBuilder(test)
.Replace("&", "&")
.Replace(" ", " ")
.Replace("<BR/>", Environment.NewLine)
.ToString();
For more exciting articles, read ASP. NET (Alex Song)
Translated from C #/. NET Little Wonders: The String Remove () and Replace () Methods