Home » .Net

How to trim trailing spaces of string using String.TrimEnd() in C#?

String.TrimEnd() Method of String Class in C#: Here, we will learn how to trim string from end (trailing)?

Given a string which contains trailing spaces and we have to trim (remove) trailing spaces.

String.TrimEnd()

This method returns string after removing the trailing spaces.

Syntax:

String String.TrimEnd();

Example:

Input string is: "   This is a sample string  "
Output string is: "   This is a sample string"

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            String str1 = "  This is a sample string  "; ;
            String str2;
           
            str2 = str1.TrimEnd();

            Console.WriteLine("Trimmed string is:(" + str2+")");

        }
    }
  
}

Output

Trimmed string is:(  This is a sample string)

Note: Input string contains spaces at starting and ending but String.TrimEnd() removes ending (trailing) spaces only.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.