ARTICLE

Trim String in VB.NET

Posted by Mahesh Chand Articles | Strings, Arrays in VB.NET August 25, 2010
This code snippet shows how to trim a string in VB.NET.
 
Reader Level:

The String class provides Trim, TrimStart and TrimEnd methods to trim strings. The Trim method removes white spaces from the beginning and end of a string. The TrimEnd method removes characters specified in an array of characters from the end of a string and TrimStart method removes characters specified in an array of characters from the beginning of a string.

You can also use the Remove method to remove characters from a string. The Listing 2 code shows how to use these methods.

    Module Module1
        Sub Main()
            Dim str As [String] = " C# "
            Console.WriteLine("Hello{0}World!", str)
            Dim trStr As String = str.Trim()
            Console.WriteLine("Hello{0}World!", trStr)
            str =
"Hello World!"
            Dim chArr As Char() = {"e"c, "H"c, "l"c, "o"c, " "c}
            trStr = str.TrimStart(chArr)
            Console.WriteLine(trStr)
            str =
"Hello World!"
            Dim chArr1 As Char() = {"e"c, "H"c, "l"c, "o"c, " "c}
            trStr = str.TrimEnd(chArr1)
            Console.WriteLine(trStr)
            Dim MyString As String = "Hello Delta World!"
            Console.WriteLine(MyString.Remove(5, 10))
            Console.ReadLine()
        End Sub
    End
Module

Output

TR1.gif

Conclusion

Hope this article would have helped you in understanding Trim String in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor