How to Remove Last Character of a string in C#
Removing the last character from a string can be done in several ways in C# programming languages: here are three ways to do it in C#:
Using .Substring Method
If you are looking for just removing the last character of a string then try this function as it is relatively easy to use.For illustration we have given an example code below that will further clarify the functionality of this function:
class RemovelastCharacter
{
static void Main()
{
string str = "Linuxhint";
string rm_character = str.Substring(0, str.Length - 1);
Console.WriteLine(rm_character);
}
}
Output
To understand the working of the example code for Substring method, here is some brief explanation for it:
rm_chracter = str.Substring(0, str.length – 1): The first argument is 0, which is the starting index of the string, whereas the second argument, length – 1, is the length of the string. This code will extract all characters of the text string except the last one, effectively removing the last character.
Below is the output for the example code we explained earlier:
Using .Remove Method
To remove the last character from a string one of the best ways is by using the .Remove() function, the main significance of this function is that one can not only remove the last character but can also remove the character from the middle or even from the start as well. For illustration we have given an example code below that will further clarify the functionality of this function:
class RemovelastCharacter
{
static void Main()
{
string str = "Linuxhint";
string rm_character = str.Remove(str.Length - 1, 1);
Console.WriteLine(rm_character);
}
}
Output
To understand the working of the example code for Substring method, here is some brief explanation for it:
rm_character = str.Remove(str.Length -1, 1): The Remove method is used to remove a specific character or sequence of characters from the string. The first argument “str.Length -1” returns the index of the last character in the string and “1” specifies that one character should be removed.
Below is the output for the example code we explained earlier:
Using .TrimEnd Method
Another way to delete the last character from a string is by using the TrimEnd() function, the main significance of this function other than that is one can remove more than one characters in the string. For illustration we have given an example code below that will further clarify the functionality of this function:
public class RemovelastCharacter
{
public static void Main()
{
string MyString = "Linuxhint";
char MyChar = 't';
string NewString = MyString.TrimEnd(MyChar);
Console.WriteLine(NewString);
}
}
Output
To understand the working of the example code for Substring method, here is some brief explanation for it:
NewString = MyString.TrimEnd(MyChar): The TrimEnd method is used to remove whitespaces or specific characters from the end of the text string. The argument myChar, specifies the character that is to be removed, which is the last character in it. This code will remove the last character of the text string.
Below is the output for the example code we explained earlier:
Conclusion
The three methods are mentioned in this guide that are commonly used to remove the last character of a string in C#. The Substring() function can be used to extract a portion of a string, the Remove() function is used to remove a specific character or sequence of characters, and the TrimEnd method is used to remove whitespaces or specific characters from the end of a string. It is important to choose the appropriate method based on the requirement of the task at hand.