如何测试是否使用了 Trim 和替换方法

how to test if Trim and Replace Method was used

我要用户输入密码。我不希望用户包含任何空格

简单的解决方案可能是这样的:

password = Console.ReadLine().Replace(" ", "")

但是,如果删除了空格,我想告诉用户有必要删除它们。但是,我该怎么做?我怎么知道替换是否做了什么?

只需将字符串存储在变量中:

Dim password As String = Console.ReadLine()
Dim cleanedPassword  As String = password.Replace(" ", "")
Dim passwordWasDirty As Boolean = cleanedPassword.Length <> password.Length

If passwordWasDirty Then
     ' tell the user....
End If