Library reference

Remove spaces at both ends

Remove the spaces before and after a name. A space between two words stays in place. These examples use ordinary spaces.

String.Trim

Simple example

Trim the single letter.

Start' a '
Resulta
Program.cs
using System;

Console.WriteLine(" a ".Trim());
Output
a
Advertisements

Everyday examples

Remove the extra spaces copied around a ticket number.

Start' B12 '
ResultB12
Program.cs
using System;

Console.WriteLine("  B12 ".Trim());
Output
B12

Clean up the outside spaces on a shelf label.

Start' Red Apples '
ResultRed Apples
Program.cs
using System;

Console.WriteLine(" Red Apples  ".Trim());
Output
Red Apples

Clean up a name copied from a signup sheet.

Start' Ana '
ResultAna
Program.cs
using System;

Console.WriteLine("  Ana ".Trim());
Output
Ana
Run the example
Terminal
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson
Official documentation