VBScript count new lines
I'm looking for a function on how to count how many new lines there are in a particular string that is being returned from the DB.
I've seen functions to do this JavaScript and PHP but none for VBScript.
I want to first check if there are Carriage Returns in the string (and count them), then I want to do a replace 开发者_运维百科on them to a <br/>
tag.
I suppose I could do the replace on vbCrLf and if it doesn't find one then there will probably be no harm done. However, for debugging purposes right now I would really like to get the count so I know what it is doing (and if it is what I'm expecting).
Any ideas on how to do this in VBScript
You can try to split the string and see the UBound of the array:
'Split the stringfile into lines
arrLines = Split(strData,vbCrLf)
lineNb = UBound(arrLines)
code adapted from here
Use a RegExp, if you want the Count and the replacement:
>> s = Join( Array( 1, 2, 3 ), vbCrLf )
>> Set r = New RegExp
>> r.Global = True
>> r.Pattern = vbCrLf
>> WScript.Echo r.Execute( s ).Count
>> WScript.Echo r.Replace( s, "<br />" )
>>
2
1<br />2<br />3
>>
If you are replacing the string and only need to know how many replacements, how about:
s = StringFromDB
l = Len(s)
s = Replace(s,vbCrLf,"<br/>")
MsgBox "Replaced: " & (Len(s)-l)/3
Once I read that for new lines chars is possible to use:
Cr (Carriage return)
Lf (Line feed)
CrLf (Both of the previous)
They are OS system related / specific: for what I know vbCrLf works in win systems platforms only, while other in linux/unix/mac OSs.
I suppose that will be better for a more general approach the use of vbNewLine instead of vbCrLf because vbNewLine adapts to the correct way as needed.
Please correct me if I'm wrong, because I am not totally sure about. tnx
精彩评论