devxlogo

Trimming strings

Trimming strings

Visual Basic .NET strings expose three trim methods: TrimStart, TrimEnd, and Trim – which trim one or more characters from the beginning, the end, or both the beginning and end of the string. They therefore work like VB6’s LTrim, RTrim, and Trim functions, with an important difference: you can decide which characters are to be trimmed. For example, in many cases you want to trim not just spaces, but also tabs and CR-LF pairs. Here’s the compact VB.NET solution:

' S is the string to be trimmed.Dim cArr() As Char = { " "c, Chr(9), Chr(13), Chr(10) }s = s.TrimStart(cArr)

You can use the same pattern with TrimEnd and Trim, of course.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist