HI WELCOME TO KANSIRIS

Remove Last Character from String in C# with Example

Leave a Comment
We have different ways to remove the last character from string in c# 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);
}

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);
}

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 = "Sirikt,";
string ostr = istr.Remove(istr.IndexOf(","));
Response.Write(ostr);
}

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

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.