|
|
| Databases 101 | | | ASP.NET Chapter | | | Suggest Article | | | Discussions | | | Bookstore | | | Write for Us | | | OpenAuction | | | Our Authors | |
|
|
|
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!
|
| |  | |  |
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 |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
|
|