Results 1 to 19 of 19

Thread: [RESOLVED] how to split text if limit over

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    91

    Resolved [RESOLVED] how to split text if limit over

    hi, how to split the line if it contain long number of alphabet.
    example:

    text1.text="hi am rahul tyagi i am from haryana."
    if i wanna make limition to display first 17 and then next
    msgbox "hi am rahul tyagi"
    msgbox "i am from haryana."


    how to do that. help me!

  2. #2
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: how to split text if limit over

    Quote Originally Posted by godfathers View Post
    hi, how to split the line if it contain long number of alphabet.
    example:

    text1.text="hi am rahul tyagi i am from haryana."
    if i wanna make limition to display first 17 and then next
    msgbox "hi am rahul tyagi"
    msgbox "i am from haryana."


    how to do that. help me!
    in simple terms you need to collect groups of 17 "valid characters"
    you can loop your way up through the words character by character
    or you could be sneaky and simply split the text into an array separating by space
    and outputting the array until the character count is exceeded
    using this fact to send the collected words and start a new collection.

    this is an idea for you to code

    come back for more once you have tried

    here to help

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    You need a rext wrapping function. You can use textwidth method to find the length in twips of a given string. So the problem changed to how you feed that string. You can feed it with a new word with one space, if that length plus the length of already measured string are less than a maximum length. The maximum length maybe is the scalewidth of a form.
    If you want ready made functions...see the source of M2000 interpreter, you can find a link in signature, search for Report command, and follow the code.There are functions for justification, center, right, left and both right and left, while we perform a wrapping. Also we can exclude some space at left and we can set the width. Also there is a function to get a result of lines needed without actually perform the output. In M2000 I use drawtext to find width of text because I use pixels to draw to a Picturebox DC, and the unicode version of the function,drawtext.

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    If the only problem is the use of left$ and mid$....
    a$=left$(t$, 17)
    b$=mid$(t$,18)
    Use a$
    make b$ the new t$
    t$=b$
    Stop when t$=""

  5. #5
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: how to split text if limit over

    Quote Originally Posted by georgekar View Post
    If the only problem is the use of left$ and mid$....
    a$=left$(t$, 17)
    b$=mid$(t$,18)
    Use a$
    make b$ the new t$
    t$=b$
    Stop when t$=""
    this does not work for sentences in msgboxes

    this does! as described in my first suggestion

    this is a vba version but you can build it in any language

    Code:
    Function makemessages(rng As range, length)
    
    Text = Split(rng, " ")
    
    ot = ""
    
    For Each t In Text
        If Len(ot) + 1 + Len(t) <= length Then
            ot = ot & " " & t
        Else
            MsgBox (ot)
            ot = t
        End If
    
    Next t
    
    If ot > "" Then MsgBox (ot)
    
    
    End Function
    hope that helps

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    Yes by splitting the text, but the splitting are in spaces only. How can you do when you have also this - for a break too?

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how to split text if limit over

    I would recommend not using variables like a$ and t$. Variable names should be descriptive of what they will hold. Using single characters is a bad habit to get into and should not be encouraged. It makes the code hard to read and hard to understand.

    As for splitting the text You probably do not want to just split it at 17 characters as that could be in the middle of a word instead you would want to check the 18th character and see what it is. If it is something other than a valid word break character then you would want to use instrrev() from that point to find the first space or other valid separator and split where that character occurs.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    91

    Re: how to split text if limit over

    i have tried
    if len(text1.text)>18 then
    msgbox text1.text ' to show first 17
    msgbox mid$(text1.text,17)
    end if
    but not working

  9. #9
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    Yes you are right about the variables names. In the example there was nothing to descriptive so they take an "name as noname". But my example was to show how we can split a string for a given number of characters. That splitting for display has no meaning.
    The OP want to show a big message from successive message boxes. This isn't a recommended way. A use must see the message in one piece. So if this message need some lines then this can be offered from a textbox. And that textbox make the wrapping automatic...
    So godfathers need a form with a text box. By using Show with the modal format he can show the form in front of other forms in his application. When his form load can read a public variable in a module with the text that has to feed the textbox. A command button can do the Unload Me and close the form.
    So one line in a module for a Public string variable, a form with textbox and command button, and a show command are what he want...

  10. #10
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    godfathers...
    you have a totally wrong view about variables...
    First forget the message box...This is not for displaying big lines.
    Try that ..you get 2 lines in one msgbox...
    msgbox "hi am rahul tyagi" & vbcrlf & "i am from haryana."


    So learn first
    Use the immediate window in IDE and do some work there. Use one letter variables to do fast checks
    write commands like
    a$="something to write there"
    ? a$
    ? mid$(a$,4,5)
    ? instr(10,a$,"th")
    ? instr(a$,"th")
    ? Right$(a$,20)
    ? Left$(a$,20)
    ? InstrRev(a$,"th")
    ? InstrRev(a$,"th", 10)
    Mid$(a$,2,3)="***"
    ? a$

  11. #11
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: how to split text if limit over

    It is for the OP to learn a piece of code...

    or to mull over an idea about how to break up a along string into smaller parts while considering the validity of the parts returned to the user interface.

    my code above breaks up the string by spaces it could as easily do so by punctuation which might include spaces and could of course be expanded to exclude hiphenation...

    the point is for the OP to learn about code and code practice.

    the old school use of i as the loop variable and j as the second has been replace in these modern times by the use of "each" negating the need for indices entirely, but the OP is new and needs to understand something first rather than run like the clappers and leave all of the foundations behind.

    have you looked through the function i wrote for you... Yes for you not gleaned from the web!

    here to help

  12. #12
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    Your code at most situation may work. If we have one word with length bigger than the limit then you display nothing...
    (I can't understand ...the verb gleaned, and what supposed to say...)
    My first message in #3 are too much for the OP and many here, so I wrote message #4. But from message #8 I was sure that the OP doesn't know basic things.
    Your function can be useful if the OP is familiar to use code from others. But I think is not.
    As for old style and new style...I think that there is nothing to do with modern ways, but with efficiency. If we can use split is good to do that. If we can't then we go a level down and do a split function as we wish. That is the spirit of programming...use tools to do.
    From some dialogues here there is a general opinion that a code with little lines, or a readable code (if isn't short), is the best practice. No and yes. No if you use the code without altering, without knowing how works, but you know what you get from that. And yes if you want to make a variant, to customize it excluding or including some methods or altering limitations.
    So what is the benefit to use "Each" and not I in a for next loop? Maybe the use of Each is a sign that you are from the new school..?? "Each" used for items when we have no indexes like 1,2,3...but there is a collection and the Each get reference from next item of that collection. Here you use it for a string array. And the benefit was? Nothing from the algorithm...view. Only that you gain 4 bytes for a variable...Is that so significant?

    So I am sure that OP must learn code practice...But cleaning a room isn't the starting point to construct a room...

  13. #13
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: how to split text if limit over

    a complete solution for the OP would involve a proper statement about what they intend the program to do and a full specification of any and all facts that the can pull together at the outset.

    a simple cobal or pascal like approach would see us teach the OP about variables,data sets and arrays and then have tehm walk character by character through the text moving pointers about the text storing pointer variables or writing and unwritting from pointer positions in to the output text in there case msgboxes.

    there is a suggestion to fetch 17 and then test the last character and then reach back into the found text and shrink it upto the first space. you would need to maintain that position as the starting point of the next text fetch and repeat until all is "eaten"

    yes there is a problem with things greater than 17chars but it is for the OP to discover and work through.

    yes they could use "/n" or vbnewline or whatever way you wish to concatenate the "17char" parts within a msgbox

    but the OP may be trying to emulate a fixed length display like those found in train stations or airports.

    here to help

  14. #14
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: how to split text if limit over

    The problem with vb is double, one for the basic code and one for ui. If anyone see for the first time a language, when showing the IDE of vb6 then has to worry about how can output something. OP wrote that from a textbox want to get some letters. From a first reading I thought that he give a simple example to clarify what he want. After my first message I send a second, because I did a late thought that the setup of the problem isn't simplify at all. To simplify the example he had to wrote about a variable not a textbox. Why he think about textbox..? Because this was the only input method he knows. So the msgbox was for output, textbox for input, and propably string functions was something new for him.
    So to make known to him the Each word to do a specific for next loop...is something over his limits, for now. I think that the ui problem can be put it back, and he must start to make immediate executions like that I suggested before. To program controls may it look easy, but to work all together isn't so easy. He can not learn a language from ui....He has to learn commands and standard things like what is a flag, what is an index, what is an offset...and what is a pointer, and some concept about addressing modes. Then he has to understand what are subroutines and functions, and how he can pass variables.And then he can go to ui, and start to using events.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    91

    Re: how to split text if limit over

    omg! are you peoples are writer?? so big paragraph
    check out this


    dim removefirst as string
    dim message as string
    message =text1.text
    do while len>17
    removefirst=mid(removefirst,1,17)
    msgbox message
    message= replace(message, removefirst, vbnullstring)
    loop

    this is working nice thank you for trying your effort to help me

  16. #16
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how to split text if limit over

    Quote Originally Posted by godfathers View Post
    omg! are you peoples are writer?? so big paragraph
    check out this


    dim removefirst as string
    dim message as string
    message =text1.text
    do while len>17
    removefirst=mid(removefirst,1,17)
    msgbox message
    message= replace(message, removefirst, vbnullstring)
    loop

    this is working nice thank you for trying your effort to help me
    How could that possibly be working? Basically it would do nothing beyond maybe throwing an error message.
    Len is the name of a function and should throw an error in that code, but VB may actually think you mean it as a variable in which case it would be = to 0 or "" in either case it will not be >17 so the loop will not execute.

    If the loop did execute then you should get an error because you are trying to read the first 17 characters of removefirst which has no value assigned to it.

    So no that is not working, not even close to working.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    91

    Re: how to split text if limit over

    Quote Originally Posted by godfathers View Post
    omg! are you peoples are writer?? so big paragraph
    check out this


    dim removefirst as string
    dim message as string
    message =text1.text
    do while len(message)>17
    removefirst=mid(message,1,17)
    msgbox message
    message= replace(message, removefirst, vbnullstring)
    loop
    msgbox message

    this is working nice thank you for trying your effort to help me
    try this @datamiser
    Last edited by goodfather; Dec 28th, 2014 at 12:56 AM. Reason: coding mistake

  18. #18
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] how to split text if limit over

    That would be better, of course it is just throwing away characters. If your message consisted of 35 characters total and the last word was Goodbye your message would be just the letter e
    It also is still not doing what you said you wanted to do in your first post.

    You probably need to give that a bit more thought and do some intelligent splitting trying to keep whole words together.
    You also should not use replace like that, sure it will work but is not the most efficient way to go about it.

    So if your message was 18 characters long do you really want to throw away the first 17 and leave only the one character? That is what the code you have shown would do.
    Maybe you would want to remove all but the last 17? If so your can use the Right$() function to get just the characters at the end of the string.

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] how to split text if limit over

    This looks like such a simple problem that I assume it is part of some training assignment. If that is true then it probably needs to be solved using only the few things that have been taught so far.

    Since we don't know what those are, we can't help a whole lot. Well, except for maybe the rather painfully Socratic approach seen above.

    I'm not sure such threads are appropriate: they could go on for 100 posts and never be of any use to future users looking for answers. It really seems like the OP needs to find a tutor locally, beg the instructor for help, etc.


    I'm all for "teaching fishing" rather than "giving fish" but this is more like... agony.

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