xyzcode
xyzcode

Reputation: 219

Trim() not working properly?

I'm building out a function to allow users to change their email address and if they accidentally put in a whitespace before or after, I need to trim it so the regex doesn't complain. It doesn't seem to work in the place I have it (the string remains exactly the same) but I'm not sure why. This is in my controller:

public ActionResult ChangeEmail(ChangeEmailVM vm)
        {
            try
            {
                if (UserManager.EmailAvail(new EmailRequest
                {
                    Email = vm.EmailAddress.Trim()
                }))
                else//...etc etc

I was thinking I'd possibly need to put .trim() in the getter or setter of the email address but I'm not sure of the proper syntax.

Upvotes: 2

Views: 2615

Answers (1)

David
David

Reputation: 10708

Keep in mind that string is read only - you cannot change the value by calling anything on it, and instead these methods return new values with the desired changes, including .Trim; if you wish to change viewModel.EmailAddress you should assign the result of Trim() to that same variable:

EDIT: At some point, I would like to see a .= operator, since this kind of "mutate and save" comes up surprisingly often

viewModel.EmailAddress = viewModel.EmailAddress.Trim()

Upvotes: 7

Related Questions