ASP.Net C# String Trim Function

Updated on 18 May 2012,
Published on 05 Jul 2008

In ASP.Net, C# String Trim Function enables you to remove the unwanted characters from both beginning and ending of the string. By Default C# string trim function trims the white spaces from both ends of the given string. Additional parameters of the String Trim function provide the functionality to remove more than one special character from both ends of the string. C# String Function cannot remove the special characters from within the string. Following are the overloads of the ASP.Net C# String Trim Function:

// 1. 
public string Trim();

// 2. 
public string Trim(params char[] trimChars);

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

First overloaded function of C# String Trim takes no argument. It removes the occurrence of white spaces by default from the beginning and end of the string. Second overloaded Trim function takes a parameter of char array type. You can pass the array of special characters to be removed from the beginning and end of the specified string.

Examples of C# String Trim Function

// string trim functions
strText = "\"ASP.Net \"C#\" Trim Function:\"";

Response.Write(strText + "<br />");

Response.Write(strText.Trim(new char[] { '"' }) + "<br />");

char[] specialChars = new char[] { '"', ':' };

Response.Write(strText.Trim(specialChars) + "<br />");

"ASP.Net "C#" Trim Function:"

ASP.Net "C#" Trim Function:

ASP.Net "C#" Trim Function


Learn with the help of live sample of C# String Trim Function from the following link:

String Trim Function

Continue to next tutorial: ASP.Net C# TrimStart String Function to learn how to remove the extra spaces and unwanted characters only from the starting positions from the string value.

0 Responses to "ASP.Net C# String Trim Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email