Trim Function in C#

Trim Function that removes the white spaces start and end from the current string.

For example :-


if the current string is "   Code solution   "and then Trim method returns "code solution"

Trim method are three types ;-

1) Trim(): removes both side white spaces from the current string (start and end from the current string )

2) TrimStart(): removes white spaces from the start from the current String.

3) TrimEnd(): removes white spaces from the end of the current string.


Example :-

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

namespace trim_method
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your first name: ");
            string firstName = Console.ReadLine();

            Console.Write("Enter your middle name or initial: ");
            string middleName = Console.ReadLine();

            Console.Write("Enter your last name: ");
            string lastName = Console.ReadLine();

            Console.WriteLine();
            Console.WriteLine("You entered '{0}', '{1}', and '{2}'.",
                              firstName, middleName, lastName);

            string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " +
                          lastName.Trim()).Trim();
            Console.WriteLine("The result is " + name + ".");
            Console.ReadLine();

        }
    }
}



Out put


Trim Function in C# Trim Function in C# Reviewed by NEERAJ SRIVASTAVA on 11:00:00 AM Rating: 5

No comments:

Powered by Blogger.