.Trim()
Anonymous contributor
Published Dec 6, 2024
Contribute to Docs
In C#, the .Trim()
method trims starting and ending whitespaces from a given string.
Syntax
str.Trim()
str
: The string from which starting and ending whitespaces are to be removed.
Example
The following example demonstrates the usage of the .Trim()
method:
using System;public class Example{public static void Main(){string str = " Codecademy ";// Using the Trim() methodstring trimmedStr = str.Trim();// Printing the resultsConsole.WriteLine($"Original: '{str}'");Console.WriteLine($"Trimmed: '{trimmedStr}'");}}
The above code produces the following output:
Original: ' Codecademy 'Trimmed: 'Codecademy'
Codebyte Example
The following codebyte example shows the use of the .Trim()
method:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C#
Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.Beginner Friendly23 hours