Home »
.Net »
C# Programs
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.
C# Basic Programs »