Results 1 to 10 of 10

Thread: Easy one.. function to break text up based on length.

  1. #1

    Thread Starter
    Lively Member Daschle's Avatar
    Join Date
    Oct 2002
    Location
    Sunnyvale, CA
    Posts
    114

    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

  2. #2
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    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"

  3. #3
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    make the program check the previous and following position for a letter that's not equal to space (whatever its called)...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim s As String
    5.    
    6.     s = "Hello this is a lot of text! Can you see how much text it is? Imagine trying to make this all one line."
    7.     MsgBox Strip(s, 30)
    8. End Sub
    9.  
    10. Private Function Strip(ByVal s As String, Length As Integer) As String
    11.     Dim i As Integer
    12.    
    13.     Strip = ""
    14.     Do
    15.         i = Length
    16.         Do While Mid$(s, i, 1) <> " "
    17.             i = i - 1
    18.             If i = 0 Then Exit Do
    19.         Loop
    20.        
    21.         Strip = Strip & Left$(s, i) & vbCr
    22.         s = Mid$(s, i + 1)
    23.     Loop Until i = 0
    24. 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.

  5. #5

    Thread Starter
    Lively Member Daschle's Avatar
    Join Date
    Oct 2002
    Location
    Sunnyvale, CA
    Posts
    114
    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

  6. #6
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    In that case, Mc_Brains code is ideal.

    Mega.
    "If at first you don't succeed, then skydiving is not for you"

  7. #7

    Thread Starter
    Lively Member Daschle's Avatar
    Join Date
    Oct 2002
    Location
    Sunnyvale, CA
    Posts
    114
    Originally posted by Mc Brain
    VB Code:
    1. Private Function Strip(ByVal s As String, Length As Integer) As String
    2. (Snip!)
    3. 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

  8. #8
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Improved version

    VB Code:
    1. Private Function Strip(ByVal s As String, Length As Integer) As String
    2.     Dim i As Integer
    3.    
    4.     Strip = ""
    5.     Do
    6.         If Len(s) > Length Then
    7.             i = Length
    8.             Do While Mid$(s, i, 1) <> " "
    9.                 i = i - 1
    10.                 If i = 0 Then Exit Do
    11.             Loop
    12.         Else
    13.             i = Length
    14.         End If
    15.        
    16.         Strip = Strip & Left$(s, i) & vbCr
    17.         s = Mid$(s, i + 1)
    18.     Loop Until i = 0 Or Len(s) = 0
    19. 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.

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Dim strLong As String
    2.     Dim strWords() As String
    3.     Dim intindex As Integer
    4.     Dim strLine As String
    5.    
    6.     strLong = "Hello this is a lot of text! Can you see how much text it is? Imagine trying to make this all one line."
    7.     strWords = Split(strLong, " ")
    8.     For intindex = 0 To UBound(strWords)
    9.         If Len(strLine & " " & strWords(intindex)) < 30 Then
    10.             strLine = strLine & strWords(intindex) & " "
    11.         Else
    12.             Debug.Print strLine
    13.             strLine = ""
    14.         End If
    15.     Next
    16.     ' Print any remaining characters.
    17.     If strLine <> "" Then
    18.         Debug.Print strLine
    19.     Else
    20.         Debug.Print strWords(intindex - 1)
    21.     End If

  10. #10
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Another function...

    VB Code:
    1. Private Function StripSplit(ByVal s As String, Length As Integer) As String
    2.     Dim i As Integer
    3.     Dim Words() As String
    4.     Dim tLen As Integer
    5.    
    6.     Words = Split(s, " ")
    7.     StripSplit = ""
    8.     tLen = 0
    9.    
    10.     For i = 0 To UBound(Words)
    11.         If tLen + Len(Words(i)) <= Length Then
    12.             StripSplit = StripSplit & Words(i) & " "
    13.         Else
    14.             StripSplit = StripSplit & vbCr & Words(i) & " "
    15.             tLen = 0
    16.         End If
    17.         tLen = tLen + Len(Words(i)) + 1 'Because of the space
    18.     Next i
    19.  
    20. 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
  •  



Click Here to Expand Forum to Full Width