
- 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 - TrimStart() Method
The C# String TrimStart() method is used to return a string after the trimming all the leading 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 TrimStart() method −
Default Syntax −
This syntax of the TrimStart method removes all leading white-space characters from the current string.
public string TrimStart();
Parametrised Syntax −
This syntax of the TrimStart method removes all leading occurrences of the specified characters from the string.
public string TrimStart(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 are removed from the beginning of the current string.
Example 1: Removing Leading WhiteSpace
Following is a basic example of the TrimStart() method to remove leading white space characters from the current string −
using System; class Program { static void Main() { string str = " Hii, tutorialspoint! "; string trimmedStr = str.TrimStart(); Console.WriteLine($"Original: '{str}'"); Console.WriteLine($"TrimmedString: '{trimmedStr}'"); } }
Output
Following is the output −
Original: ' Hii, tutorialspoint! ' TrimmedString: 'Hii, tutorialspoint! '
Example 2: Trim Specific Leading Characters
Let's look at another example. Here, we use the parametrized TrimStart() method to remove the specified special character from start of the string −
using System; class Program { static void Main() { string str = "!!Hello, tutorialspoint!!"; string trimmedStr = str.TrimStart('!', ','); Console.WriteLine($"Original: '{str}'"); Console.WriteLine($"TrimmedString: '{trimmedStr}'"); } }
Output
Following is the output −
Original: '!!Hello, tutorialspoint!!' TrimmedString: 'Hello, tutorialspoint!!'
Example 3: Sanitizing User Input
In this example, we use the TrimStart() method to sanitize user input by removing or modifying unsafe characters from starting of the user input −
using System; class Program { public static void Main() { string userInput = " admin"; if (userInput.TrimStart() == "admin") { Console.WriteLine("Valid input"); } else{ Console.WriteLine("Invalid Input"); } } }
Output
Following is the output −
Valid input
Example 4: Check for WhiteSpace or Specified Char
The example below checks if there is whitespace or specified characters at the beginning of the string 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."); } if (str.Contains('!') || str.Contains(',')) { Console.WriteLine("The string contains '!' or ',' characters."); } string trimmedStr = str.TrimStart('!', ' '); Console.WriteLine($"Original String: '{str}'"); Console.WriteLine($"TrimStartmed String: '{trimmedStr}'"); } }
Output
Following is the output −
The string contains whitespace. The string contains '!' or ',' characters. Original String: ' !!tutorialspoit, Hello World' TrimStartmed String: 'tutorialspoit, Hello World'