dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 41

Thread: How to "untrim" variable?

  1. #1
    John K. Guest

    How to "untrim" variable?

    Hi!

    Lets say I want to make my string variable 10 characters long.
    I want to give it it´s value and then fill the restt with spaces.

    I could do like this:

    MyString = "Hello "

    But lets say I dont know what text to fill it with.
    I've tried this, but it doesnt work:

    For n = 1 to rnd * 6
    MyString1 = MyString1 & chr(rnd * 100)
    Next n
    MyString2 = Space$(10)
    MyString2 = MyString1

    But as I said, that doesn't work at all.
    Is there anyway to fill a string which length you dont know with spaces so
    it reaches the length of 10?

    Thanks!

    --
    John Knoop
    E-mail: JOHN.K@HOME.SE
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ



  2. #2
    Mattias Sjögren Guest

    Re: How to "untrim" variable?

    John,

    >Is there anyway to fill a string which length you dont know with spaces so
    >it reaches the length of 10?


    If I understand your question correctly:

    ResultString = Left$(YourString & Space$(10), 10)


    Mattias

    ____________________________________________
    Mattias Sjögren - mattiass @ hem.passagen.se
    http://hem.spray.se/mattias.sjogren/
    Docendo discimus

  3. #3
    Phil Weber Guest

    Re: How to "untrim" variable?

    > Is there anyway to fill a string which length you don't
    > know with spaces so it reaches the length of 10?


    John: Try this:

    MyString = Space$(10)
    LSet MyString = "Text"

    or this:

    Dim MyString As String * 10
    MyString = "Text"

    ---
    Phil Weber



  4. #4
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    .. . . or, in addition to the other responses, consider this:

    MyString = Format$("Hello", "!@@@@@@@@@@")

    or in the more general form:

    MyString = "Hello"
    FieldLength = 10
    MyString = Format$(MyString, "!" & String$(FieldLength, "@"))


    Rick


    "John K." <john.k@home.se> wrote in message news:390b688d@news.devx.com...
    > Hi!
    >
    > Lets say I want to make my string variable 10 characters long.
    > I want to give it it´s value and then fill the restt with spaces.
    >
    > I could do like this:
    >
    > MyString = "Hello "
    >
    > But lets say I dont know what text to fill it with.
    > I've tried this, but it doesnt work:
    >
    > For n = 1 to rnd * 6
    > MyString1 = MyString1 & chr(rnd * 100)
    > Next n
    > MyString2 = Space$(10)
    > MyString2 = MyString1
    >
    > But as I said, that doesn't work at all.
    > Is there anyway to fill a string which length you dont know with spaces so
    > it reaches the length of 10?
    >
    > Thanks!
    >
    > --
    > John Knoop
    > E-mail: JOHN.K@HOME.SE
    > ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    >
    >




  5. #5
    Phil Weber Guest

    Re: How to "untrim" variable?

    > Format$(...)

    Slooooow!
    ---
    Phil Weber



  6. #6
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    This was a race <g> ? We are only moving a 4-character string within a
    10-character field . . . what's the rush? As my post said, "in addition to
    the other responses". As a matter of fact, two additional alternatives

    Either

    Dim MyString As String * 10
    MyString = "Text"

    or

    MyString = Space$(10)
    Mid$(MyString, 1) = "Text"


    Rick


    "Phil Weber" <pweber@teleport.com> wrote in message
    news:390b7852$1@news.devx.com...
    > > Format$(...)

    >
    > Slooooow!
    > ---
    > Phil Weber
    >
    >




  7. #7
    Michael Culley Guest

    Re: How to "untrim" variable?


    Just to add another way to do it, just for fun

    MyString = "Text"
    if len(MyString)<=10 then MyString = MyString & space$(10-len(MyString))

    Mike

    "Rick Rothstein" <rick_newsgroup@email.com> wrote:
    >This was a race <g> ? We are only moving a 4-character string within a
    >10-character field . . . what's the rush? As my post said, "in addition

    to
    >the other responses". As a matter of fact, two additional alternatives
    >
    >Either
    >
    > Dim MyString As String * 10
    > MyString = "Text"
    >
    >or
    >
    > MyString = Space$(10)
    > Mid$(MyString, 1) = "Text"
    >
    >
    >Rick
    >
    >
    >"Phil Weber" <pweber@teleport.com> wrote in message
    >news:390b7852$1@news.devx.com...
    >> > Format$(...)

    >>
    >> Slooooow!
    >> ---
    >> Phil Weber
    >>
    >>

    >
    >



  8. #8
    Phil Weber Guest

    Re: How to "untrim" variable?

    > This was a race? <g>

    Rick: It should *always* be a race. Given two techniques that accomplish the
    same thing and are equally readable, why would one ever choose the slower
    one?

    Here are the comparative times of five of the techniques that have been
    suggested in this thread (times in milliseconds; code compiled to native
    code with VB6 SP3, all optimizations applied; benchmark code attached):

    Fixed-Length String: 20
    LSet: 80
    Mid$: 90
    Left$: 190
    Format$: 841

    Format$ is over 40 times slower than using a fixed-length string; for what
    possible reason would you choose to use it in this case?
    ---
    Phil Weber


    Attached Files Attached Files

  9. #9
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    First off, those posted times are a little misleading. Someone reading them
    without examining your code would conclude it takes more than 3/4 seconds to
    execute one Format$ statement. Those times appear to be the time required to
    execute 100,000 iterations of each statement. Hence, if it took 841
    milliseconds to execute 100,000 Format$ statements, then it only took
    0.00841 of second to execute one of them. True, the other techniques are
    much faster; but I still doubt in normal usage that this "slowness" would be
    noticeable nor burdensome.

    As to why use it -- well, the Format statement is a very versatile and hence
    an easily remembered statement construction-wise. I'll bet that John K, once
    being exposed to all of the posted techniques, will find the Format
    statement the easiest to understand when he reads his code a month from now.
    I assume that based on the skill level I think he is currently at (which is
    based on the fact that he asked the initial question that he did). I'll
    grant you that the fixed length string would be as easily remembered also,
    but its length is fixed and can't be modified within the running program (as
    can all the other solutions). It just seemed that exposing him to the Format
    statement, a tool that is so useful in so many different ways, would prove
    beneficial to him at (what I believe is) his current skill level.

    Obviously the above is just my opinion. You may feel free to disagree.

    Rick



    "Phil Weber" <pweber@teleport.com> wrote in message
    news:390bd48d$1@news.devx.com...
    > > This was a race? <g>

    >
    > Rick: It should *always* be a race. Given two techniques that accomplish

    the
    > same thing and are equally readable, why would one ever choose the slower
    > one?
    >
    > Here are the comparative times of five of the techniques that have been
    > suggested in this thread (times in milliseconds; code compiled to native
    > code with VB6 SP3, all optimizations applied; benchmark code attached):
    >
    > Fixed-Length String: 20
    > LSet: 80
    > Mid$: 90
    > Left$: 190
    > Format$: 841
    >
    > Format$ is over 40 times slower than using a fixed-length string; for what
    > possible reason would you choose to use it in this case?
    > ---
    > Phil Weber
    >
    >




  10. #10
    Phil Weber Guest

    Re: How to "untrim" variable?

    > Obviously the above is just my opinion. You may
    > feel free to disagree.


    Rick: If it's your position that it makes sense to use slower code when
    doing so provides no real advantage, and when it would require no more
    effort to use an optimal technique, then yes, I disagree. Hopefully,
    however, this discussion has proved informative to those who are interested
    in optimizing their code, and for that I thank you.
    ---
    Phil Weber



  11. #11
    John K. Guest

    Re: How to "untrim" variable?

    OK, thank you all who have helped me on this one.
    (Mattias, Phil, Rick and Michael)

    "Phil Weber" <pweber@teleport.com> wrote in message
    news:390bf35c@news.devx.com...
    > > Obviously the above is just my opinion. You may
    > > feel free to disagree.

    >
    > Rick: If it's your position that it makes sense to use slower code when
    > doing so provides no real advantage, and when it would require no more
    > effort to use an optimal technique, then yes, I disagree. Hopefully,
    > however, this discussion has proved informative to those who are

    interested
    > in optimizing their code, and for that I thank you.
    > ---
    > Phil Weber
    >
    >




  12. #12
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    > If it's your position that it makes sense to use slower
    > code when doing so provides no real advantage


    You seem to have ignored the point I made about skill-level. I have nothing
    against code optimization and in fact employ its techniques quite a lot. But
    (and I don't want John K to be insulted by this analogy) if a child asks you
    "how do I multiply these two numbers together?", do you give him a
    calculator and say push these buttons and then walk away? Or do you show
    that child how to do the process long hand, showing how what they know
    relates to the process and perhaps showing how it might apply to other math
    questions to come (like division)?


    > and when it would require no more effort to use
    > an optimal technique, then yes, I disagree.


    I still believe John K is better served by being introduced to the Format
    statement and its various "flavors" than to LSet. Go back and look at the
    question he asked AND look at the solution he said he tried but which didn't
    work for him. Look at that attempted solution carefully and tell me you
    think the "light bulb of understanding" went off in his head when he read
    your suggestion and, in order to understand it better (rather than blindly
    "push the keys on the calculator"), consulted the LSet example and read the
    following:

    This example uses the LSet statement to left align a string within a
    string variable. Although LSet can also be used to copy a variable
    of one user-defined type to another variable of a different, but
    compatible, user-defined type, this practice is not recommended.
    Due to the varying implementations of data structures among
    platforms, such a use of LSet can't be guaranteed to be portable.

    Dim MyString
    MyString = "0123456789" ' Initialize string.
    Lset MyString = "<-Left" ' MyString contains "<-Left ".


    > Hopefully, however, this discussion has proved
    > informative to those who are interested in
    > optimizing their code


    Based on the question he asked, and I believe this *has* to be taken into
    consideration, I don't think John K is ready to be make optimization the
    overridding concern in his programming efforts. Rather, getting comfortable
    with the "major" commands and how to use them effectively with each other
    should be. Format, while admittedly slow, is amazingly flexible. He can
    investigate optimization techniques as well as the "lesser known" commands
    later, when he is ready.


    > and for that I thank you.


    You're welcome.




  13. #13
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    And I forgot to include this correction in my post:

    In the previous post where I said "Hence, if it took 841 milliseconds to
    execute 100,000 Format$ statements, then it only took 0.00841 of second to
    execute one of them." . . . it should have read 0.00841 of a MILLISECOND
    since the original 100,000 iterations were timed in milliseconds. Therefore,
    one Format statement executed in 0.00000841 of a second under your test.
    That maybe slow in comparison to other those other commands, but nearly
    instaneous by human standards.

    Rick



  14. #14
    Phil Weber Guest

    Re: How to "untrim" variable?

    > You seem to have ignored the point I made about
    > skill-level.


    Rick: I think less-experienced developers are better served by being taught
    which techniques are inefficient so they can avoid getting into the habit of
    using them. Format$ is such a poor performer that I believe it should be
    avoided except in cases where it saves a great deal of effort on the
    developer's part. This is not such a case. It would have been a disservice
    to John (and other readers of this thread) to have not pointed out the
    performance differences between Format$ and the other solutions presented.
    ---
    Phil Weber



  15. #15
    Phil Weber Guest

    Re: How to "untrim" variable?

    > That maybe slow in comparison to other those other
    > commands, but [is] nearly instantaneous by human
    > standards.


    Rick: Fine. You still haven't explained to me why I should use a slower
    technique when it costs nothing to use a much faster one.
    ---
    Phil Weber



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center