Introduction : In this tutorial we will learn how remove or trim white spaces from a given string using Trim() method of string class in .NET Framework using C# and vb.net code example.
Using Trim() method you can remove the white or extra spaces from the given string. Remember it will remove the extra space at the starting and/or ending not at the middle of the words. This is always a good practice to using Trim() method while talking the value from text box in the server side.
C# Code sample: using System;
namespace DotNetSpark.Tutorials
{
class DotNetSpark
{
static void Main(string[] args)
{
// Declare string containing white spaces;
string strValue = " DotNetSpark ";
//using Trim() method of string class remove the extra space from the given string and display
Console.WriteLine(strValue.Trim());
Console.ReadLine();
}
}
}
Output:
DotNetSpark
VB.NET Code example: Namespace DotNetSpark.Tutorials
Class DotNetSpark
Private Shared Sub Main(args As String())
' Declare string containing white spaces;
Dim strValue As String = " DotNetSpark "
'using Trim() method of string class remove the extra space from the given string and display
Console.WriteLine(strValue.Trim())
Console.ReadLine()
End Sub
End Class
End Namespace
Output:
DotNetSpark
|