VB.net trim spaces off both ends of a string


What VB.net code would I use to trim spaces off both ends of a string?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
Lokesh B RDeveloperCommented:

Dim input As String = "  Hello  "
Dim output As String = input.Trim()
' Output - "Hello"

Open in new window

Dim output As String = input.TrimStart()
' will remove the beginning spaces - "Hello  "

Dim output As String = input.TrimEnd()
' "  Hello"

käµfm³d 👽Commented:
Just a minor point of contention:  Trim and its cousins will trim all whitespace, not merely spaces. So if you were planning on keeping leading/trailing newlines, tabs, etc., then you need to use the overload of Trim which takes a character to trim.


Dim result As String = original.Trim(" "c)

Open in new window

Most often when people say, "trim spaces off both ends," they mean whitespace. But just in case you don't, I thought I'd clarify  = )
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both and thanks for the extra info kaufmed
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.