VB.net trim spaces off both ends of a string

Hi

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:
Hi,

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"

https://msdn.microsoft.com/en-us/library/t97s7bs3%28v=vs.110%29.aspx
0
 
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.

e.g.

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  = )
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both and thanks for the extra info kaufmed
0
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.