jammer20002
06-05-2004, 02:13 PM
I need a bit of help in using VBScript.
I've got a string and need to split the string every 150 characters.
Also, I want to replce newline characters with commas.
I know it must be really simple... and I can do it using looads of other more complex languages like Perl!
But I'm stuck. :(
Can someone help me out with the code?
It should only be one or two lines.
Thanks.
Jam
buntine
06-08-2004, 08:41 PM
Replacing the newline characters with commas should be a one liner.
Dim strDelimited
'| VBScript uses the vbCrLf statement apposed to the \n escape sequence.
strDelimited = Replace(originalString, vbCrLf, ",")
As for spliting the string every 150 characters, it would be bst to write your own function. This one will return an array of strings.
Dim strMessage
Dim arrStrings()
Dim i
strMessage = "Some massive string"
arrStrings = SplitString(strMessage)
If IsArray(arrStrings) Then
For i = 0 To UBound(arrStrings) - 1
Response.Write(arrStrings(i) & "<br />")
Next
End If
Function SplitString(strOriginal)
Dim arr()
Dim i, intToSplit, count
intToSplit = 150
count = 0
'| Make sure the passed parameter is long enough
If Len(strOriginal) < intToSplit Then
SplitString = Nothing
End If
'| Split the string into blocks.
For i = 0 to Len(strOriginal) / intToSplit
ReDim Preserve arr(i)
arr(i) = Mid(strOriginal, count, intToSplit)
count = count + intToSplit
Next
'| Get the left over characters and put them into a new array index.
Dim intLeftOver
intLeftOver = Len(strOriginal) Mod intToSplit
If intLeftOver <> 0 Then
ReDim Preserve arr(i+1)
arr(i+1) = Right(strOriginal, intLeftOver)
End If
SplitString = arr '| Return the array.
End Function
Ok, that should work. But if it does return an error, just post here again and i will fix up my mistakes.
Regards,
Andrew Buntine.