c sharp

How to use Trim() Function in C#

When working with strings in C#, it’s important to know how to manipulate them effectively. One of the most common operations is trimming, which removes whitespace characters from the beginning and end of a string.

In this article, we will explore the various ways to use the C# Trim() method and its variants to achieve optimal string manipulation results.

What is Trim() in C#

In C#, the Trim() is a function that returns a new string and is used to remove all the white spaces or characters from a specific string, and if there are no whitespaces, the method returns the string without changing it. It is the System.String type method and can be overloaded by passing arguments to it.

Syntax for Trim() in C#

Below is the syntax for using the Trim() function in C#:

public string Trim();

The first syntax will not take any argument:

You can also use the Trim() function in the following way with arguments:

public string Trim (params char[] trimChars);

The above method will take an array of Unicode characters or null parameters.

The following are the overloaders of the Trim() method:

Method Description
Trim(Char[]) All the leading and trailing sets of characters specified in an array will be removed.
Trim() All the white spaces from the string will be removed.

How String.Trim() Works in C#

The following examples will explain the working of the Trim() function in C#:

Example 1: Trimming Whitespaces from the Beginning and End of a String

To remove whitespaces from the beginning and end of a string, follow the below-given code:

using System;

class myClass {

static void Main() {

string str1 = " Hello LinuxHint! ";

string str2 = str1.Trim();

Console.WriteLine("Original string: "" + str1 + """);

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

}

}

Example 2: Trimming Specific Characters from the Beginning and End of a String

Here is a code that uses the Trim() function to trim specific characters from the beginning and end of a string in C#.

using System;

class myClass

{

static void Main(string[] args)

{

string str1 = "$$$Hello LinuxHint!$$$";

char[] trimChars = { '$', '!' };

string str2 = str1.Trim(trimChars);

Console.WriteLine(str1);

Console.WriteLine(str2);

}

}

Example 3: Trimming Characters from the String

This method is used to remove the characters from the string:

using System;

public class program {

public static void Main(String[] args) {

string str = "LinuxHint!";

char[] arr = { 'L', 'i'};

Console.WriteLine("String = "+str);

Console.WriteLine("String after Trim() method call = " + str.Trim(arr));

}

}

Bottom Line

The Trim() method in C# is an essential tool to remove white spaces and specific characters from a string, and it can be easily implemented using different syntaxes and overloads. Whether you are a beginner or an experienced developer, learning how to use Trim() effectively can help improve your coding skills and productivity.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.