How do I remove trainling spaces for a compare?

  • Thread starter Thread starter BobM
  • Start date Start date
B

BobM

I have two strings that I would like to compare. To guarantee the compare is
not affected by trailing spaces, I would like to remove them.

Will this do: MyTextBox.text.ToString().Trim()?
 
Hi BobM,

Yes, this method removes both trealing and leading whitespaces. If you want
to consider only trailing spaces for removing use TrimEnd method. If you
want to remove only leading spaces use TrimStart. Bear in mind that this
methods return a new string. The original one is not affected. Strings are
immutable.
 
It depends. Your code returns the trimmed contents of MyTextBox.Text, but it
doesn't actually trim the contents of the textbox. To do that, you would
need to use
MyTextBox.Text = MyTextBox.Text.Trim()

I don't think you need the 'ToString()' bit.

Hope that helps!
 
Back
Top