Remove Last Character from String in C#, VB.NET with Example

Introduction: 

Here I will explain how to remove last character from string in c#, vb.net with example or delete or remove last character in string with examples in c#, vb.net. In c# or vb.net we can easily remove last character from string by using Remove or Trim or IndexOf properties.
Description: 
  
 
In previous posts I explained send list as parameter to function with example, check if arraylist contains specific string or not, jQuery display image based on the url entered,  jQuery Remove first or last characters from string, Delete selected rows from datatable in c#, vb.net and many articles relating to Asp.net, Gridview, Ajax, JQuery. Now I will explain how to remove last character from string in c#, vb.net with example.
We have different ways to remove the last character from string in c#, vb.net based on our requirements.

Following are the few methods which we can use to remove last character from string.


Method 1
Following is the one way of removing last character from string using Remove property

C# Code

protected void Page_Load(object sender, EventArgs e)
{
string istr = "1,2,3,4,5,6,7,8,9,10,";
string ostr = istr.Remove(istr.Length - 1, 1);
Response.Write(ostr);
}


VB.NET



Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim istr As String = "1,2,3,4,5,6,7,8,9,10,"
Dim ostr As String = istr.Remove(istr.Length - 1, 1)
Response.Write(ostr)
End Sub


Method 2

Following is the another way of removing last character from string using Trim property

C# Code


protected void Page_Load(object sender, EventArgs e)
{
string istr = "1,2,3,4,5,6,7,8,9,10,";
string ostr = istr.Trim(",".ToCharArray());
Response.Write(ostr);
}



VB.NET


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim istr As String = "1,2,3,4,5,6,7,8,9,10,"
Dim ostr As String = istr.Trim(",".ToCharArray())
Response.Write(ostr)
End Sub


Method 3

Following is the another way of removing the last character from string using IndexOf property but that character should exists only one time otherwise this property will return the values before the first character.

C# Code


protected void Page_Load(object sender, EventArgs e)
{
string istr = "ebmmstech,";
string ostr = istr.Remove(istr.IndexOf(","));
Response.Write(ostr);
}


VB.NET


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim istr As String = " ebmmstech,"
Dim ostr As String = istr.Remove(istr.IndexOf(","))
Response.Write(ostr)
End Sub



This is how we can remove last character from string in c#, vb.net based on our requirements.

No comments:

Post a Comment

Flipkart