Vbscript 修剪函数

标签 vbscript whitespace space trim blank-line

我有一个读取逗号分隔文本文件的脚本,但是每当我对文件中提取的值之一使用 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

我的脚本从未达到“匹配”,我不知道为什么。

最佳答案

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
Next

关于Vbscript 修剪函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39258260/

相关文章:

scripting - 使用 VBScript 读取 CSV 文件

vbscript - VBScript中的列表

java - 用空格分隔列表项,用选项卡分隔更高级别的指标

c# - 将字符串写入不带格式的 XML 文件 (C#)

firefox - TinyMCE无法捕获空格键/空格键

com - 在 vista 64 上执行脚本后 cscript.exe 崩溃

vbscript - 如何知道在VBscript中找不到的文件的文件名

Java - 将多个路径作为命令行参数空格问题

C++ boost::qi 将空格和换行符分隔的数字解析为二维 vector

vb.net 如何将带空格的字符串传递到命令行