|
-
Dec 17th, 2002, 07:24 PM
#1
Thread Starter
Lively Member
Easy one.. function to break text up based on length.
I'm trying to write a small function (if one doesn't already exist) to break up long lines of text into CRLF seperated chunks.
Example:
The inputted text is "Hello this is a lot of text! Can you see how much text it is? Imagine trying to make this all one line."
The function would notice that it's longer than 30 characters, so it needs to break it up to approx 30 letters per line...
"Hello this is a lot of text! "
"Can you see how much te"
"xt it is? Imagine trying to "
"make this all one line."
However, obviously that's no good..
So, basically, I need a function which would notice that it's the middle of the word and adjust accordingly. My code so far is, frankly, a horrible mess so I'll spare you the pain of having to witness my ineptitude. 
Any suggestions?
Thank you.
"It's difficult to imagine a world in which people voluntarily choose to listen to liberals. There is no evidence that it has ever happened. " - Ann Coulter
-
Dec 17th, 2002, 07:33 PM
#2
Frenzied Member
What controls are you using to both input the text and then output. Do you want to send the results to a printer or another text box?
Have you tried playing a with a text box with multiline set to true?
Please elaborate and we will help. 
Mega.
"If at first you don't succeed, then skydiving is not for you"
-
Dec 17th, 2002, 07:38 PM
#3
Frenzied Member
make the program check the previous and following position for a letter that's not equal to space (whatever its called)...
-
Dec 17th, 2002, 07:38 PM
#4
Need-a-life Member
VB Code:
Option Explicit
Private Sub Form_Load()
Dim s As String
s = "Hello this is a lot of text! Can you see how much text it is? Imagine trying to make this all one line."
MsgBox Strip(s, 30)
End Sub
Private Function Strip(ByVal s As String, Length As Integer) As String
Dim i As Integer
Strip = ""
Do
i = Length
Do While Mid$(s, i, 1) <> " "
i = i - 1
If i = 0 Then Exit Do
Loop
Strip = Strip & Left$(s, i) & vbCr
s = Mid$(s, i + 1)
Loop Until i = 0
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Dec 17th, 2002, 07:39 PM
#5
Thread Starter
Lively Member
Originally posted by Mega_Man
Have you tried playing a with a text box with multiline set to true?
Please elaborate and we will help. 
Here's some background..
I have a button. When you mouse over the button, it brings up a label object which is redrawn below the button.. like a tooltip, but different.
The label text is populated by an ini file I've read in.. normally the ini contents would be small, but if it runs over 90 chars, the label scrolls off the screen.
To achieve this, I'm basicaly envisioning a ToolTipCaption(text as string) function or something..
Assume TEXT is the text read in by the ini deal. Assume it's more than 90 chars.
Basically the program would pass this TEXT to the function and the function would return "Sometext" vbcrlf "Moretext" vbcrlf "moretext" etc..
Easy enough to do, if you don't mind the fact that I can't see an easy way to make the function check to see if you're cutting up a word, and if so, to move to the end of the word before inserting the vbcrlf.
Hope that made sense...
"It's difficult to imagine a world in which people voluntarily choose to listen to liberals. There is no evidence that it has ever happened. " - Ann Coulter
-
Dec 17th, 2002, 07:43 PM
#6
Frenzied Member
In that case, Mc_Brains code is ideal. 
Mega.
"If at first you don't succeed, then skydiving is not for you"
-
Dec 17th, 2002, 07:48 PM
#7
Thread Starter
Lively Member
Originally posted by Mc Brain
VB Code:
Private Function Strip(ByVal s As String, Length As Integer) As String
(Snip!)
End Function
Woot! Thanks. That'll do it. That's just about the utter, complete opposite of how I was picturing doing it in my head.. clearly I still have much to learn (duh.)
Thanks agian.
If anyone else is bored and feels like throwing out suggestions, I'm always open to looking at different ways of doing things.. best way to learn is to see how others have done it.
"It's difficult to imagine a world in which people voluntarily choose to listen to liberals. There is no evidence that it has ever happened. " - Ann Coulter
-
Dec 17th, 2002, 07:49 PM
#8
Need-a-life Member
Improved version
VB Code:
Private Function Strip(ByVal s As String, Length As Integer) As String
Dim i As Integer
Strip = ""
Do
If Len(s) > Length Then
i = Length
Do While Mid$(s, i, 1) <> " "
i = i - 1
If i = 0 Then Exit Do
Loop
Else
i = Length
End If
Strip = Strip & Left$(s, i) & vbCr
s = Mid$(s, i + 1)
Loop Until i = 0 Or Len(s) = 0
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Dec 17th, 2002, 07:51 PM
#9
VB Code:
Dim strLong As String
Dim strWords() As String
Dim intindex As Integer
Dim strLine As String
strLong = "Hello this is a lot of text! Can you see how much text it is? Imagine trying to make this all one line."
strWords = Split(strLong, " ")
For intindex = 0 To UBound(strWords)
If Len(strLine & " " & strWords(intindex)) < 30 Then
strLine = strLine & strWords(intindex) & " "
Else
Debug.Print strLine
strLine = ""
End If
Next
' Print any remaining characters.
If strLine <> "" Then
Debug.Print strLine
Else
Debug.Print strWords(intindex - 1)
End If
-
Dec 17th, 2002, 07:55 PM
#10
Need-a-life Member
Another function...
VB Code:
Private Function StripSplit(ByVal s As String, Length As Integer) As String
Dim i As Integer
Dim Words() As String
Dim tLen As Integer
Words = Split(s, " ")
StripSplit = ""
tLen = 0
For i = 0 To UBound(Words)
If tLen + Len(Words(i)) <= Length Then
StripSplit = StripSplit & Words(i) & " "
Else
StripSplit = StripSplit & vbCr & Words(i) & " "
tLen = 0
End If
tLen = tLen + Len(Words(i)) + 1 'Because of the space
Next i
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|