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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: