Remove Last Character from String in C#

The C# String.Remove method can be used to remove last characters from a string in C#. The String.Remove method in C# creates and returns a new string after removing a number of characters from an existing string.

C# String.Remove() method has two overloaded forms: 

  1. Remove(Int32) - Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
  2. Remove(Int32, Int32) - Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.

Remove Last Character from a C# String

The following code example removes the last character from a string. 

/** Remove sample **/  
string founder = "Mahesh Chand is a founder of C# Corner!";  
// Remove last character from a string  
string founderMinus1 = founder.Remove(founder.Length - 1, 1);  
Console.WriteLine(founderMinus1);  

Remove All Characters After a Character Position

The following code example all characters after 25 characters. 

// Remove all characters after first 25 chars  
string first25 = founder.Remove(25);  
Console.WriteLine(first25); 

Remove All Characters In Between A C# String

The following code example removes 12 characters after 10th position in a string. 

// Remove characters start at 10th position, next 12 characters  
String newStr = founder.Remove(10, 12);  
Console.WriteLine(newStr);  
int pos = founder.IndexOf("founder");  
if (pos >= 0)  
{  
    // String after founder  
    string afterFounder = founder.Remove(pos);  
    Console.WriteLine(afterFounder);  
    // Remove everything before founder but include founder  
    string beforeFounder = founder.Remove(0, pos);  
    Console.Write(beforeFounder);  
}

Here is a Tutorial on C# String

Here is the complete source code of use of the C# String.Replace method. 

using System;
namespace RemoveStringSample {
    class Program {
        static void Main(string[] args) {
            /** Remove sample **/
            string founder = "Mahesh Chand is a founder of C# Corner!";
            // Remove last character from a string  
            string founderMinus1 = founder.Remove(founder.Length - 1, 1);
            Console.WriteLine(founderMinus1);
            // Remove all characters after first 25 chars  
            string first25 = founder.Remove(25);
            Console.WriteLine(first25);
            // Remove characters start at 10th position, next 12 characters  
            String newStr = founder.Remove(10, 12);
            Console.WriteLine(newStr);
            int pos = founder.IndexOf("founder");
            if (pos >= 0) {
                // String after founder  
                string afterFounder = founder.Remove(pos);
                Console.WriteLine(afterFounder);
                // Remove everything before founder but include founder  
                string beforeFounder = founder.Remove(0, pos);
                Console.Write(beforeFounder);
            }
            Console.ReadKey();
        }
    }
}