I would like to know to remove spaces from start and end of a string without removing the middle space. Ex.

_ _String _ Sample _ _ = String sample

I have a code:

Text1.text = Replace(Text1, " ", "")

The code removes all the space in a text. Ex. _ _String_Sample_ _ = StringSample

I want the middle space to remain. Only the first and the last space will be removed.

Dani AI

Generated

Quick expert note: is correct — trimming is the right approach when you only want to remove leading and trailing whitespace and keep interior spaces intact. The Replace approach removes every space and will collapse words together, which is why it behaved incorrectly for .

Common, safe approaches (different VB environments)

' VB.NET: use the instance Trim() method (removes Unicode whitespace at ends)
Text1.Text = Text1.Text.Trim()
' Classic VB (VB6): use LTrim and RTrim if you prefer explicit left/right trimming
Text1.Text = LTrim(RTrim(Text1.Text))

If you need to be stricter (remove tabs, NBSP, other Unicode separators) use a regex to trim any kind of whitespace:

' VB.NET example using Regex to strip all leading/trailing whitespace
Text1.Text = System.Text.RegularExpressions.Regex.Replace(Text1.Text, "^\s+|\s+$", "")

Notes and gotchas tied to the thread

  • Null/Nothing: in .NET, calling instance methods on Nothing throws an exception — check for Nothing or use String.IsNullOrEmpty before trimming.
  • Proper-case + trim: trim first, then apply a title/proper-case conversion. That prevents stray leading/trailing characters from affecting the result. In .NET prefer Culture-aware conversion (TextInfo.ToTitleCase) for better language handling.
  • Nonbreaking spaces: HTML-derived input can include nonbreaking spaces (U+00A0) that some simple trims or replacements miss. The regex above or a targeted replace for ChrW(160) will handle those.
  • Interior spacing: trimming does not alter interior spacing. If you also want to normalize multiple interior spaces to a single space, that is a separate step and should be done deliberately.

This keeps your textbox text clean without destroying the spaces between words, and avoids the unintended effects seen when using Replace to strip every space.

Recommended Answers

All 4 Replies

Use the Trim function, as in -

Dim MyStrin As String

MyString = Trim$(Text1.Text)

Text1.Text = MyString

This will remove ONLY the first and last spaces

Thank you so much. . I just thought of it. . .I keep on using trim but I really don't know what its do. . .Funny me. . One question.

Is this code valid?

Trim$(StrConv(Text1.Text, vbProperCase))

I want to trim a Textbox which string is converted in Proper Case.

Yes it is. I have stuffed up a little with the code at

Trim&

Remove the & from trim. The Strconvert is perfect, as in -

Dim str As String

str = " nagatron "
Text1.Text = Trim(StrConv(str, vbProperCase))

This will return Nagatron, with no spaces.

Has this solved your problem Nagatron? If so, please mark this as solved, found at the bottom of this page, thanks.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.