This is not a solution to the problem you are asking about, but may still solve it for you and should be done anyhow IMO:
Why don't you apply a Trim
to the username before processing it?
username = username.Trim();
This should remove any white space at the beginning and end and so the problematic situation should never happen.