我有一个读取逗号分隔的文本文件的脚本,但是每当我在文件中提取的一个值上使用Trim(str)时,它将不起作用……
我的文本文件:
some string, anotherstring, onelaststring
some string, anotherstring, onelaststring
some string, anotherstring, onelaststring
some string, anotherstring, onelaststring我的脚本:
Dim fso, myTxtFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set myTxtFile = fso.OpenTextFile("mytxt.txt")
Dim str, myTxtArr
txtContents myTxtFile.ReadAll
myTxtFile.close
myTxtArr = Split(txtContents, vbNewLine)
For each line in myTxtArr
tLine = Split(tLine, ",")
Trim(tLine(1))
If tLine(1) = "anotherstring" Then
MsgBox "match"
End If
Next我的脚本从来没有达到“匹配”,我也不确定为什么。
发布于 2016-09-01 04:26:25
Trim()是一个返回修剪后的字符串的函数。您的代码不正确地使用它。需要使用返回值:
myTxtArr(1) = Trim(myTxtArr(1))或者使用另一个变量来存储值,并在比较中使用该单独的变量,
trimmedStr = Trim(myTxtArr(1))
If trimmedStr = "anotherstring" Then或者您可以直接在比较中使用函数返回值,
If Trim(myTxtArr(1)) = "anotherstring" Then以下是该部分代码的更正版本:
For each line in myTxtArr
tLine = Split(line, ",")
tLine(1) = Trim(tLine(1))
If tLine(1) = "anotherstring" Then
MsgBox "match"
End If
Nexthttps://stackoverflow.com/questions/39258260
复制相似问题