For loop 如何检测新线?
我尝试逐字符读取字符串,并检测是否有新行,如果是这种情况,则创建一个输出For loop 如何检测新线?,for-loop,vbscript,For Loop,Vbscript,我尝试逐字符读取字符串,并检测是否有新行,如果是这种情况,则创建一个输出 strText = "A;B;C" & vbcrlf & "D;E;F" wscript.echo strText For i=1 To Len(strText) charx = Mid(strText,i,1) if charx = "\n" then wscript.echo "OMG, NEW LINE DETECTED!!!" end if Next
strText = "A;B;C" & vbcrlf & "D;E;F"
wscript.echo strText
For i=1 To Len(strText)
charx = Mid(strText,i,1)
if charx = "\n" then
wscript.echo "OMG, NEW LINE DETECTED!!!"
end if
Next
我尝试将读取的字符与
“\n”
进行比较,但失败了。标识新行的简单方法是使用环境。换行符标识新行的简单方法是使用环境。换行符
if charx = vbLf then
wscript.echo "OMG, NEW LINE DETECTED!!!"
end if
在vbscript中,“\n”
是一个包含两个字符的字符串,没有新行字符
在vbscript中“\n”
是一个包含两个字符的字符串,没有新行字符使用InStr
函数,如下所示:
option explicit
'On Error Resume Next
On Error GoTo 0
Dim strText, strResult
strResult = Wscript.ScriptName
strText = "A;B;C" & vbcrlf & "D;E;F;vbCrLf"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
strText = "A;B;C" & vbNewLine & "D;E;F;vbNewLine"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
strText = "A;B;C" & vbCr & "D;E;F;vbCr"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
strText = "A;B;C" & vbLf & "D;E;F;vbLf"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
Wscript.Echo strResult
Wscript.Quit
Function testCrLf( sText)
If InStr(1, sText, vbCrLf, vbBinaryCompare) Then
testCrLf = "CrLf detected in "
Else
testCrLf = "CrLf not found in "
End If
End Function
输出:
==>cscript D:\VB_scripts\SO\32411401.vbs
32411401.vbs
--------------------
CrLf detected in A;B;C
D;E;F;vbCrLf
--------------------
CrLf detected in A;B;C
D;E;F;vbNewLine
--------------------
D;E;F;vbCround in A;B;C
--------------------
CrLf not found in A;B;C
D;E;F;vbLf
==>
使用InStr
功能如下:
option explicit
'On Error Resume Next
On Error GoTo 0
Dim strText, strResult
strResult = Wscript.ScriptName
strText = "A;B;C" & vbcrlf & "D;E;F;vbCrLf"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
strText = "A;B;C" & vbNewLine & "D;E;F;vbNewLine"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
strText = "A;B;C" & vbCr & "D;E;F;vbCr"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
strText = "A;B;C" & vbLf & "D;E;F;vbLf"
strResult = strResult & vbNewLine & String(20, "-") & vbNewLine & testCrLf( strText) & strText
Wscript.Echo strResult
Wscript.Quit
Function testCrLf( sText)
If InStr(1, sText, vbCrLf, vbBinaryCompare) Then
testCrLf = "CrLf detected in "
Else
testCrLf = "CrLf not found in "
End If
End Function
输出:
==>cscript D:\VB_scripts\SO\32411401.vbs
32411401.vbs
--------------------
CrLf detected in A;B;C
D;E;F;vbCrLf
--------------------
CrLf detected in A;B;C
D;E;F;vbNewLine
--------------------
D;E;F;vbCround in A;B;C
--------------------
CrLf not found in A;B;C
D;E;F;vbLf
==>
我尝试了如果charx=Environment.NewLine,那么
,但是我得到了一个错误。我尝试了如果charx=Environment.NewLine,那么
,但是我得到了一个错误。谢谢,这很有效。我以前也用vbCrLf
尝试过,但没有成功。@EdwardBlack,它不能用vbCrLf
工作,因为这个常量是一个两个字符的字符串,一个回车符后跟一个换行符,但是你正在迭代字符串,每次提取一个字符,一个字符串不能等于两个字符串。谢谢,这会有帮助。谢谢,这很有效。我以前也用vbCrLf
尝试过,但没有成功。@EdwardBlack,它不能用vbCrLf
工作,因为这个常量是一个两个字符的字符串,一个回车符后跟一个换行符,但是你正在迭代字符串,每次提取一个字符,一个字符串不能等于两个字符串。谢谢,这会有所帮助。