-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Term Entry] C# Strings: .Trim() (#5770)
* [Term Entry] C# Strings: .Trim() * Update trim.md fixed codes ---------
- Loading branch information
1 parent
c246b7a
commit 44d373e
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
Title: '.Trim()' | ||
Description: 'Trims starting and ending whitespaces from a given string.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Methods' | ||
- 'Functions' | ||
- 'Strings' | ||
- 'Characters' | ||
CatalogContent: | ||
- 'learn-c-sharp' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
In C#, the **`.Trim()`** [method](https://www.codecademy.com/resources/docs/c-sharp/methods) trims starting and ending whitespaces from a given string. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
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: | ||
|
||
```cs | ||
using System; | ||
|
||
public class Example | ||
{ | ||
public static void Main() | ||
{ | ||
string str = " Codecademy "; | ||
|
||
// Using the Trim() method | ||
string trimmedStr = str.Trim(); | ||
|
||
// Printing the results | ||
Console.WriteLine($"Original: '{str}'"); | ||
Console.WriteLine($"Trimmed: '{trimmedStr}'"); | ||
} | ||
} | ||
``` | ||
|
||
The above code produces the following output: | ||
|
||
```shell | ||
Original: ' Codecademy ' | ||
Trimmed: 'Codecademy' | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
The following codebyte example shows the use of the `.Trim()` method: | ||
|
||
```codebyte/csharp | ||
using System; | ||
public class CodebyteExample | ||
{ | ||
public static void Main() | ||
{ | ||
string str = " Programming "; | ||
// Using the Trim() method | ||
string trimmedStr = str.Trim(); | ||
// Printing the results | ||
Console.WriteLine($"Original: '{str}'"); | ||
Console.WriteLine($"Trimmed: '{trimmedStr}'"); | ||
} | ||
} | ||
``` |