Remove First Character From String in C#
-
Remove the First Character From String With the
String.Remove()
Method in C -
Remove the First Character From String With the
String.Substring()
Method in C
This tutorial will discuss the methods to remove the first character from a string in C#.
Remove the First Character From String With the String.Remove()
Method in C
The String.Remove(x, y)
method in C# removes a string value of a specified length and start index from the original string. It returns a new string in which the characters from index x
and having length y
are removed from the original string. We can pass 0
as starting index and 1
as the length to the String.Remove()
method to remove the first character from a string.
using System;
namespace remove_first_character
{
class Program
{
static void Main(string[] args)
{
string s1 = "this is something";
s1 = s1.Remove(0, 1);
Console.WriteLine(s1);
}
}
}
Output:
his is something
In the above code, we removed the first character from the string variable s1
with the s1.Remove(0, 1)
method in C#.
Remove the First Character From String With the String.Substring()
Method in C
We can also achieve the same goal using the String.Substring()
method. The String.Substring(x)
method gets a smaller string from the original string that starts from the index x
in C#. We can pass 1
as the starting index to remove the first character from a string.
using System;
namespace remove_first_character
{
class Program
{
static void Main(string[] args)
{
string s1 = "this is something";
s1 = s1.Substring(1);
Console.WriteLine(s1);
}
}
}
Output:
his is something
In the above code, we removed the first character from the string variable s1
with the s1.Substring(1)
method in C#. This approach is slightly faster than the previous way, but the difference is not drastic and noticeable.