
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# String - Trim() Method
The C# String Trim() method is used to return a string after the trimming all the leading and trailing white space characters or (specified character) from a string. This method does not modified the original string but returns a new string with the changes.
Syntax
Following are the syntax of the C# string Trim() method −
Default Syntax −
This syntax of the Trim method removes all leading and trailing white-space characters from the current string.
public string Trim();
Parametrised Syntax −
This syntax of the Trim method removes all leading and trailing occurrences of the specified characters from the string.
public string Trim(params char[]? trimChars);
Parameters
This method accepts the following parameters −
- trimChars: This is an array of Unicode characters to remove, or null.
Return Value
This method returns a string that remains after all occurrences of the characters in the trimChars parameter have been removed from the start and end of the current string.
Example 1: Removing Leading and trailing WhiteSpace
Following is a basic example of the Trim() method to remove leading and trailing white space characters from the current instance of string −
using System; class Program { static void Main() { string str = " Hii, tutorialspoint! "; string trimmedStr = str.Trim(); Console.WriteLine($"Original: '{str}'"); Console.WriteLine($"Trimmed: '{trimmedStr}'"); } }
Output
Following is the output −
Original: ' Hii, tutorialspoint! ' Trimmed: 'Hii, tutorialspoint!'
Example 2: Trimming Specific Characters
Let's look at another example. Here, we use the parametrized Trim() method to remove the specified special character from the current string −
using System; class Program { static void Main() { string str = "!!Hello, tutorialspoint!!"; string trimmedStr = str.Trim('!', ','); Console.WriteLine($"Original: '{str}'"); Console.WriteLine($"Trimmed: '{trimmedStr}'"); } }
Output
Following is the output −
Original: '!!Hello, tutorialspoint!!' Trimmed: 'Hello, tutorialspoint'
Example 3: Sanitizing User Input
In this example, we use the Trim() method to sanitize user input by removing or modifying unsafe characters from user input before being used in an application −
using System; class Program { public static void Main() { string userInput = " admin "; if (userInput.Trim() == "admin") { Console.WriteLine("Valid input"); } else{ Console.WriteLine("Invalid Input"); } } }
Output
Following is the output −
Valid input
Example 4: Check for White-space or Specified Characters
The below example, checks if a string contains white-space or specified characters before trimming it −
using System; class Program { static void Main() { string str = " tutorialspoit, Hello, World!! "; // Check for whitespace if (str.Contains(' ')) { Console.WriteLine("The string contains whitespace."); } // Check for specific characters (e.g., '!' or ',') if (str.Contains('!') || str.Contains(',')) { Console.WriteLine("The string contains '!' or ',' characters."); } // Trim whitespace and specified characters string trimmedStr = str.Trim('!', ' '); Console.WriteLine($"Original String: '{str}'"); Console.WriteLine($"Trimmed String: '{trimmedStr}'"); } }
Output
Following is the output −
The string contains whitespace. The string contains '!' or ',' characters. Original String: ' tutorialspoit, Hello, World!! ' Trimmed String: 'tutorialspoit, Hello, World'