INSTR 函数中逗号的含义是什么?



我目前的任务是改进现有的VBA文件。但是,我无法与执行此代码的人联系。那么我可以知道 first = InStr(2, sl, ",", 1( 中的逗号是什么意思吗?因为我去在 csv 文件中查找逗号,但我根本找不到使用 ctrl F 并在字符串之间手动搜索逗号。

Private Sub readdata(filename As String, i As Integer)
Application.ScreenUpdating = False
Dim sl As String
Dim first As Integer
Dim second As Integer
Dim j As Long
j = 2
Set fs2 = CreateObject("Scripting.FileSystemObject") 
Set o_file = fs2.OpenTextFile(filename, 1, TristateFalse) 

Do While Left(sl, 1) = "#"   
sl = o_file.readline
Loop
Do While o_file.atendofstream <> True 'atendofstream = Read-only property that returns True if the file pointer is at the end of a TextStream file; False if it is not.
sl = o_file.readline
first = InStr(2, sl, ",", 1)  
second = InStr(first + 2, sl, ",", 1)
If second = 0 Then
second = Len(sl) + 1
End If

InStr 功能

","正在字符串中搜索真正的逗号sl

是的,CSV表示逗号分隔值,但并不意味着所有CSV文件中都有一个真正的逗号。这可以解释为什么它返回 0。

InStr 的语法如下:

InStr([ start ], string1, string2, [ compare ])

所以InStr(2, sl, ",", 1)搜索sl中的,,其中起始位置是2,最后的1意味着存在文本比较。

请注意,.csv经常被另一个字符分隔,而不是,,例如;

InStr( [start], string, substring, [compare] (

开始:可选。它是搜索的起始位置

字符串:要在其中搜索的字符串。

子字符串:要查找的子字符串。

比较:可选。这是要执行的比较类型

最新更新