C# String Trim()

In C#, the Trim() method is used to remove leading and trailing white space characters from a string. The method returns a new string with the leading and trailing white space characters removed. The original string is not modified.

Here’s an example usage of the Trim() method:

string text = "   Hello, World!   ";
string trimmedText = text.Trim();

Console.WriteLine(trimmedText); // Output: "Hello, World!"

In this example, the Trim() method is called on the text string, removing the leading and trailing white space characters. The resulting trimmed string, "Hello, World!", is then printed to the console.

It’s important to note that Trim() only removes white space characters like spaces, tabs, and newlines. If you want to remove specific characters or a set of characters from the beginning or end of a string, you can use the Trim(params char[]) overload. This overload allows you to specify the characters you want to remove.

Here’s an example using the Trim(params char[]) overload:

string text = "+++Hello, World!+++";
char[] trimChars = { '+', '!' };
string trimmedText = text.Trim(trimChars);

Console.WriteLine(trimmedText); // Output: "Hello, World"

In this example, the Trim(params char[]) overload is used to remove the + and ! characters from the beginning and end of the text string. The resulting trimmed string, "Hello, World", is then printed to the console.

Signature:

The signature of the Trim() method in C# is as follows:

public string Trim();

This signature indicates that the Trim() method does not take any parameters and returns a string object. It can be called on an instance of the string class.

Here’s an example of using the Trim() method with its signature:

string text = "   Hello, World!   ";
string trimmedText = text.Trim();

In this example, the Trim() method is called on the text string, and the trimmed result is assigned to the trimmedText variable.

Parameter:

The Trim() method in C# does not take any parameters. It is used to remove leading and trailing white space characters from a string.

Here is the correct signature and example of using the Trim() method:

Signature:

public string Trim();

Example:

string text = "   Hello, World!   ";
string trimmedText = text.Trim();

In this example, the Trim() method is called on the text string, and it removes the leading and trailing white space characters. The resulting trimmed string is then assigned to the trimmedText variable.

C# String Trim() Method Example:

The Trim() method in C# returns a new string that is a copy of the original string with leading and trailing white space characters removed. It does not modify the original string in-place.

Here’s an example to illustrate the return value of the Trim() method:

string text = "   Hello, World!   ";
string trimmedText = text.Trim();

Console.WriteLine(trimmedText); // Output: "Hello, World!"

In this example, the Trim() method is called on the text string. The returned value, which is the trimmed version of the original string, is assigned to the trimmedText variable. When we print the trimmedText variable to the console, it displays the trimmed string “Hello, World!”.

It’s important to note that the Trim() method does not modify the original string. If you want to change the original string itself, you need to assign the trimmed value back to the original variable:

string text = "   Hello, World!   ";
text = text.Trim();

Console.WriteLine(text); // Output: "Hello, World!"

In this case, the original text variable is updated with the trimmed value, so subsequent use of text will reflect the trimmed string.