如何检测新线?

时间:2015-09-05 09:16:08

标签: for-loop vbscript

我尝试通过char读取字符串char,并检测是否有新行,如果是这种情况则创建输出。

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 

我通过将readed char与"\n"进行比较来尝试它,但这失败了。

3 个答案:

答案 0 :(得分:2)

if charx = vbLf then
    wscript.echo "OMG, NEW LINE DETECTED!!!"
end if

在vbscript "\n"中是一个包含两个字符的字符串,没有换行符号

答案 1 :(得分:2)

使用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

==>

答案 2 :(得分:-1)

识别新行的简单方法是使用Environment.NewLine