www.webdeveloper.com
Results 1 to 2 of 2

Thread: VBScript help needed.

  1. #1
    Join Date
    Sep 2003
    Posts
    218

    VBScript help needed.

    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

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Replacing the newline characters with commas should be a one liner.
    Code:
    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.
    Code:
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles