Best Ways To Use String Remove In C#

String Remove Banner Image

Introduction

C# Remove does as it says, it remove text from another string. But how does it work when a string is immutable? It makes a copy of the current string and leaves out the text to be removed. This function is good for automated processes but it often needs other string functions to search for positions of strings like IndexOf. It is compact, easy to use, and has very few function overloads.

What is String Remove?

String remove takes an existing string and based on the start index and length for removal then will return a string with that section removed. The function overloads allow two options. To give a start index and the rest of the string is removed or to give a start index. String remove is the inverse of the substring method.

Function Overload Syntax 1
    public string Remove(int startIndex)

This is the simplest form of string removal with just one parameter. A start index is provided and it will remove text from that point onto the end of the string. See an example usage below.

string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
int index = 17;//Start index to remove
string updatedText = text.Remove(index);//Removing from the index to the end of the string and leaving the rest.
Console.WriteLine(updatedText);//Write to screen
Code Output
user_name,user_id

In the example above, if we just wanted to see the user information and exclude the rest then we put the start index right at the comma point and the remove function will remove the rest will be returned. Next, we'll look at the next function overload.

Function Overload 2
public string Remove(int startIndex, int count)

This second function overload takes an index and also the count from the index in which to remove. Check out the code snippet below.

string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
int index = 37;//Start index to remove
int count = 5;//How many characters to remove
string updatedText = text.Remove(index, count);//Removing from the index and count 
Console.WriteLine(updatedText);//Write to screen
Code Output
user_name,user_id,latitude,longitude,map_data,point

In this example, we remove the null, from the string by starting on the n of the null and the count extending to the comma.

Exceptions

Index Out of Range

This exception occurs when the start index does not exist within the original string either less than 0 or greater than the length of the string. So it's a good idea to place if statements to check the bounds of the index so that this exception doesn't happen. See the same code below.

string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
int index = 358;//Start index to remove
string updatedText = text.Remove(index);//Removing from the index to the end of the string and leaving the rest.
Console.WriteLine(updatedText);//Write to screen

Result

Out of Range Exception Image

In this example, the start index is greater than the length of the string so an exception is thrown. Next, we need to add protection against this use case so it is handled in the code.

Protection Against Index Out of Range Exception

To handle this use case, a wrapper function will be created and the bounds of the index will be checked before calling the string remove function. Examine the code below.

string RemoveText(int index, string originalText)
{
    if (index < 0 || index > originalText.Length - 1)//check the bounds of the string
    {
        return originalText;//return original string if startIndex is out of the bounds
    }
    string newText = originalText.Remove(index);//safe to call this now
    return newText;
}
string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
int startIindex = 358;//Start index to remove
string updatedText = RemoveText(startIindex, text);//Removing from the index to the end of the string and leaving the rest.
Console.WriteLine(updatedText);//Write to screen
Code Output
user_name,user_id,latitude,longitude,null,map_data,point

Notice that the output is what we started with. This means that the index was out of bounds and the use case handled and we didn't alter the string. This is expected, that now no exception is thrown.

When Might This Function Be Used?

Unwanted Text and Repeated Patterns

This function is great for removing unwanted text at certain index positions when the pattern repeats. Even dynamically with indexOf find a character at a certain position and then remove the unwanted characters.

Readability

Having a function that says remove makes it clear what is happening in the code. This is important when working with other people and everyone is working on the same code base to have readable code. There are other ways of getting rid of text from a string but none of them say remove.

Alternative Methods of Remove

There are other ways of removing text from a string. Using the string replace method can be used but it is not as precise as picking specific sections to remove text from. It has the advantage of being compact if you know which characters or strings you want to want to eliminate ahead of time. Or if you want all instances of that type of text delete from the string this would be good to use. See the example below.

string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
string updatedText = text.Replace("null,", "");//replace all instances of null, in the string to empty
Console.WriteLine(updatedText);//Write to screen

How String Remove Works

Below are two examples of how this function may work. At the core, this function loops through all the characters in the string and includes and excludes the text based on the start index. Function overload one shows that all the characters are looped through and added to a new string until the start index is reached. Function overload two includes all characters until the start index is reached. Then it includes all characters of the after-start index plus the count.

Function Overload 1 Example
string RemoveText(string originalText, int startIndex)
{
    if (startIndex < 0 || startIndex > originalText.Length - 1)//check the bounds of the string
    {
        return originalText;//return original string if startIndex is out of the bounds
    }
    string newText = "";
    for (int i = 0; i < originalText.Length; i++)//loop through each character
    {
        if (i < startIndex)//add characters to a new string until it is less than the remove index
        {
            newText += originalText[i];//add text to the new string
        }
    }
    return newText;
}
string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
int startIndex = 17;//Start index to remove
string updatedText = RemoveText(text, startIndex);//Removing from the index to the end of the string and leaving the rest.
Console.WriteLine(updatedText);//Write to screen
Code Output
user_name,user_id
Function Overload 2 Example
string RemoveText(string originalText, int startIndex, int count)
{
    if (startIndex < 0 || startIndex > originalText.Length - 1)//check the bounds of the string
    {
        return originalText;//return original string if startIndex is out of the bounds
    }
    string newText = "";
    for (int i = 0; i < originalText.Length; i++)//loop through each character
    {
        if (i < startIndex)//add characters to a new string until it less than the remove index
        {
            newText += originalText[i];//add text to the new string
        }
        else if (i >= startIndex + count)//add characters to the new string after the start plus the count
        {
            newText += originalText[i];//add text to the new string
        }
    }
    return newText;
}
string text = "user_name,user_id,latitude,longitude,null,map_data,point";//Starting string
int startIndex = 37;//Start index to remove
int count = 5;
string updatedText = RemoveText(text, startIndex, count);//Removing from the index to the end of the string and leaving the rest.
Console.WriteLine(updatedText);//Write to screen
Code Output
user_name,user_id,latitude,longitude,map_data,point

Conclusion

String remove is compact, fast, and easy to use and works well within the system of the other string functions. It is usually not just used by itself and usually is used alongside IndexOf. So remove is good at getting rid of text and a specific position and length.

Get Latest Updates