Internet.comReal World Active Server Pages Solutions and Resources 
ASPWatch.com
Sponsored by Compuware Corporation
Essential survival skills for Internet success!
ScriptingDatabaseComponentsXMLIntegrationSolutions

Newsletter
Click here!
Search


Pete Nelson's Home Page
Log off

internet.com
Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers


Internet Jobs
Affiliate Solutions
Manage Your E-Store
Buy Cameras for Less
Web System Integrator
Training Solutions
IT Solutions
Add Chat To My Site
Add Online Sales Reps
Volume Licensing

WordWrap Function
by Pete Nelson
Skill level: Beginner
First posted: Friday, June 09, 2000
WordWrap Function

Here's a handy function that you can use to add word-wrapping to a large portion of text. Just pass in the text you want to wrap, how long each line should be, the text used for wrapping (such as vbCrLf or "<br>" if you're doing HTML), and a true/false as to whether or not the function should strip out any carriage-returns (vbCrLf) before wrapping the text. It will return a string with the wrapping characters embedded at the proper positions.



'------------------------------------------------------------------------------
' WordWrap, author: Pete Nelson weasel@ecis.com
'
' Parameters :
'   strWords - Words to add wrapping to
'   intWrapLength - Length of each "line"
'   str WrapText - Text to wrap words with, such as <br> or vbCrLf
'   blnReplaceVbCrLf - True/False, remove vbCrLf from strWords?
'
' Purpose :
'   Returns a string embedded with the strWrapText character, depending
'   on intWrapLength. Used to insert vbCrLf or <br> in large amounts of
'   text for better formatting
'
'------------------------------------------------------------------------------

Function WordWrap(ByVal strWords, ByVal intWrapLength, ByVal strWrapText, ByVal blnReplaceVbCrLf)

    Dim arrWords, arrTrailingCharacters, x
    Dim intRunningLength


    '*** Strip out carriage returns
    If blnReplaceVbCrLf Then
        strWords = Replace(strWords, vbCrLf, " ")
    End If


    '*** Split the words into an array using a space. The
    '*** second array just makes it easier to add the Wrap Text
    arrWords = Split(strWords, " ")
    arrTrailingCharacters = Split(strWords, " ")

    '*** Set the trailing characters for each word to a space
    For x = LBound(arrTrailingCharacters) To UBound(arrTrailingCharacters)
        arrTrailingCharacters(x) = " "
    Next


    '*** Now start looping through the words and adding the wrap text
     intRunningLength = 0

    For x = LBound(arrWords) To UBound(arrWords)

        '*** Calculate the running length of the words
        intRunningLength = intRunningLength + Len(arrWords(x) & " ")

        '*** If we're at the exact word wrap length, add the wrapping text
        '*** at the end of the current word and reset the running length
        If intRunningLength = intWrapLength Then
            arrTrailingCharacters(x) = strWrapText
            intRunningLength = 0
        End If


        '*** If we've pass the wrapping length, put the wrapping text
        '*** at the end of the previous word. Set the running length
        '*** to the length of the current word.
        If intRunningLength >= intWrapLength And x > 0 Then
            arrTrailingCharacters(x - 1) = strWrapText
            intRunningLength = Len(arrWords(x) & " ")
        End If

    Next

    '*** Build the words and the wrapping text back together and return them
     For x = LBound(arrWords) To UBound(arrWords)
        WordWrap = WordWrap & arrWords(x) & arrTrailingCharacters(x)
    Next

End Function


WordWrap in Action

Give it a try - WordWrap the Gettysburg Address!

Download the attachment: wordwrap.asp

Pete Nelson
Pete is the webmaster for DVD Tracker, a site that lets people keep an online catalog of their DVD collection. He has been doing ASP development for three years and is proficient in VBScript, SQL and HTML, and dabbles in VB COM, PHP and mySql now and then. He's married to a Linux geek and lives in the San Francisco Bay Area.
What did you think of this article?
Not useful Very useful
Badly writtenWell written
Too shortToo long
This document can be found in these Encyclopedia chapters:
º VBScript
This is what people have been saying about this article:
dont work - Anonymous - Wednesday January 3 3:40:00 PM
thanks!!! - Anonymous - Saturday December 30 4:32:00 PM

Create a new discussion
View Article Statistics

Authors...
Edit this article
View Preview Version
Printable Copy of Article
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Essential survival skills for Internet success!

About your privacy | Want to advertise? | Contact Us

Copyright © internet.com Corporation 2001
http://www.internet.com