我从text103.text开始,其文本值为我要检查的C:\ test.txt.因此,如果text103.text中的任何内容与C中的任何内容匹配:\ test.txt label3.caption应该读取"成功",但每次运行它时,我都会"失败"为什么?
所以这是我的按钮中的代码:
Private Sub Command1_Click()
nFileNum = FreeFile
Open "C:\test.txt" For Input As nFileNum
lLineCount = 1
Do While Not EOF(nFileNum)
Line Input #nFileNum, sNextLine
sNextLine = sNextLine & vbCrLf
sText = sText & sNextLine
Loop
Text102.Text = sText
Close nFileNum
If Text102.Text = Text103.Text Then
Label3.Caption = "success"
Else
Label3.Caption = "failure"
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
即使我的text103.text以"hello"开头,我编辑C:\ test.txt只是说"你好"它总是给我label3.caption"失败"!!! 为什么???
可能是因为您总是在从文件读取的数据中添加换行符.
是否也Text103.Text包含新行?
更新:
vbCrLf又\r\n是空白字符集的一部分,因此您可能无法直接看到它们.
在If Text102.Text = Text103.Text Then尝试之前,
msgbox "Len 102 " & Len(Text102.Text) & " Len 103 " & Len(Text103.Text)
这将显示字符串是不同的长度,因此它们不能相等.
或者,在立即模式下尝试? "[" & text102.Text & "]"并? "[" & text103.Text & "]"假设有问题的单词是"你好",我敢打赌第一个将打印
[你好
]
和第二个
[你好]