.NET interview questions: – Can we use Trim function in C#?

Trim: – The Trim function helps us to remove OFF spaces
characters from the beginning and from the end of the instance.

The following are the different variation’s provided by the Trim
function.

1. Trim () = It simply removes OFF Spaces from both the
Start
and End of the instance or string.

2. TrimStart () = It simply removes OFF Spaces from the Start
of the instance or string.

3. TrimEnd () = It simply removes OFF Spaces from the End
of the instance or string.

Let’s see a small and simple example to understand the concept of the above mentioned Trim function with their respective variations.

In order to see it practically just follow the following steps.

Step1: – Create a Console Application for that just Open Visual
Studio
>> go to >> File >> New >> Project >> Windows >> Select Console Application.


Step2: – Now, let take the above variation one by one and demonstrate.

1. Trim () = It simply removes OFF Spaces from both the
Start
and End of the instance or string.

Now just simply add the below code snippet to your program.cs
file.

static void Main(string[] args)
{
// Created a Simple String Variable with it's Value.
string strMyData = " Performing the Trim Function example. ";
// Call Trim instance method.
// This returns a new string copy.
strMyData = strMyData.Trim();
// Added a Bracket So that we can see the exact performance of Trim.
Console.WriteLine("["+strMyData+"]");
// The below line of code will help us to see the output window.
Console.ReadLine();
}

In the above code snippet you have noticed that the string variable
strMyData
contains value with some OFF Spaces at the start and
at the end of the data.

Now, let simply run the Console application and will see the result like below diagram.

In the above output you can clearly see that now the OFF spaces are removed from the start and end of the string.

Step3: – Similarly let’s take the second variation and see it
practically.

2. TrimStart () = It simply removes OFF Spaces from the Start
of the instance or string.

Now just simply add the below code snippet to your program.cs file.

static void Main(string[] args)
{
// Created a Simple String Variable with it's Value.
string strMyData = " Performing the Trim Function example. ";
// Call TrimStart instance method.
// This returns a new string copy.
strMyData = strMyData.TrimStart();
// Added a Bracket So that we can see the exact performance of Trim.
Console.WriteLine("["+strMyData+"]");
// The below line of code will help us to see the output window.
Console.ReadLine();
}

Now, let simply run the Console application and will see the result
like below diagram.

In the above output diagram you can clearly see that the OFF Spaces are removed from the starting of the string but the end of the OFF Spaces are still not removed which means that TrimStart() function only removes the OFF spaces from the beginning of the string.

Step4: – Now, let’s take the final variation provided by the Trim function and demonstrate it practically.

3. TrimEnd () = It simply removes OFF Spaces from the End
of the instance or string.

Now just simply add the below code snippet to your program.cs
file.

static void Main(string[] args)
{
// Created a Simple String Variable with it's Value.
string strMyData = " Performing the Trim Function example. ";
// Call TrimEnd instance method.
// This returns a new string copy.
strMyData = strMyData.TrimEnd();
// Added a Bracket So that we can see the exact performance of Trim.
Console.WriteLine("["+strMyData+"]");
// The below line of code will help us to see the output window.
Console.ReadLine();
}

Now, let simply run the Console application and will see the result like
below diagram.

Now, from the output diagram you can clearly see that the OFF spaces from the end of the string are removed while the OFF Spaces from the start are not removed, which means that the TrimEnd() basically removes only the OFF Spaces from the end of the string.

This is how exactly Trim function help us to remove OFF spaces from the
string.

Also see the following video on params keyword used in C#: –


See for more stuffs on  interview questions and answers for .NET

Regards,

See more stuffs on author’s blog for Most asked Dotnet interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

2 Responses to .NET interview questions: – Can we use Trim function in C#?

  1. rizzi1234 says:

    informative post.. I’m preparing for .net interview questions online at http://www.wiziq.com/course/46-INTERVIEW-PREPARATION-COURSE-BY-SHIVPRASAD-KOIRALA and so was looking for such materials to enhance my knowledge .. thanks

  2. Mohan says:

    Thanks for your tutorial. Up to now i didn’t have any knowledge on Trim methods. when ever we observing this tutorial i could understood easily.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s