Share Blog

Wednesday, June 20, 2018

Remove Last Character from String in C#


Here I will explain how to remove last character from string in C#,with example or delete or remove last character in string with examples in C#.We can easily remove last character from string by using Remove or Trim or IndexOf properties.

Method 1

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

string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Remove(inputstring.Length - 1, 1);

Console.Write(outputstring);

Console.ReadLine();


Method 2

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


string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Remove(inputstring.LastIndexOf(","));

Console.Write(outputstring);

Console.ReadLine();


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.


string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Trim(",".ToCharArray());

Console.Write(outputstring);

Console.ReadLine();


No comments:

Post a Comment