;

How To Remove the Last Word from the String in C#


Tutorialsrack 10/06/2020 C# Programs

In this article, you will learn how to remove the last word from the string in C#. There are many ways to remove the last word from the string in C#. In this article, we used the String.Substring() and String.Remove() method of the String class. 

Here are the examples to remove the last word from the string in c#.

Here is the original String:  “Welcome to the tutorialsrack .com

Output String We Need:  “Welcome to the tutorialsrack”

Example 1: Using String.Substring() Method.

In this example, we used the String.Substring() method of the String Class.  String.Substring() method is used to retrieve the substring from this instance of the string. This method is overloaded. With this method, we used the String.LastIndexOf() method to get the last index of the specified character from where we remove the substring and String.TrimEnd() method to trim the substring from the instance of the string.

Here the is source code of the program to remove the last word from the string using String.Substring() method in C#.

Example 1 - Using String.Substring() Method
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How To Remove the Last Word from the String using
           String.Substring() method in C# */
        static void Main(string[] args)
        {
            string str = "Welcome to the tutorialsrack .com ";

            // remove the space from the start 
            // and at the end of the string
            str = str.Trim();
            Console.WriteLine("Original String is: {0}",str);

            string newStr = "";
            if (str.Contains(" "))
            {
                newStr = str.Substring(0, str.LastIndexOf(' ')).TrimEnd();
            }
            //After Removing the Last Word from specific character, the string is
            Console.WriteLine("\nFinal String is: {0}", newStr);
            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output

Original String is: Welcome to the tutorialsrack .com

 

Final String is: Welcome to the tutorialsrack

Example 2: Using String.Remove() Method

In this example, we used the String.Remove() method of the String class. String.Remove() method is used to delete characters in the string starting from the specified position and continues to the end of the string. This method is overloaded. With this method, we used the String.LastIndexOf() method to get the last index of the specified character from where we remove the substring and String.TrimEnd() method to trim the substring from the instance of the string.

Here is the source code of the program to remove the last word from the string using String.Remove() method in C#.

Add Little Description About Code

Example 2 - Using String.Remove() Method
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How To Remove the Last Word from the String using
           String.Remove() method in C# */
        static void Main(string[] args)
        {
            string str = "Welcome to the tutorialsrack .com ";

            // remove the space from the start 
            // and at the end of the string
            str = str.Trim();
            Console.WriteLine("Original String is: {0}",str);

            string newStr = "";
            if (str.Contains(" "))
            {
                newStr = str.Remove(str.LastIndexOf(' ')).TrimEnd();
            }
            //After Removing the Last Word from specific character, the string is
            Console.WriteLine("\nFinal String is: {0}", newStr);
            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output

Original String is: Welcome to the tutorialsrack .com

 

Final String is: Welcome to the tutorialsrack

I hope this article will help you to understand how to remove the last word from the string in C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags